Ejemplo n.º 1
0
        public void ChangeExtensionTest()
        {
            var fileName = "test_" + MethodBase.GetCurrentMethod().Name + ".txt";

            File.Create(TestDir + fileName).Close();

            var newExtension = ".data";

            var file = new FileInfo(TestDir + fileName);

            file.ChangeExtension(newExtension);

            Assert.AreEqual(newExtension, Path.GetExtension(file.FullName));
        }
Ejemplo n.º 2
0
        public void ChangeExtension()
        {
            // Type
            var @this        = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_IO_FileInfo_ChangeExtension.txt"));
            var @thisNewFile = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_IO_FileInfo_ChangeExtension.cs"));

            // Intialization
            using (FileStream stream = @this.Create())
            {
            }

            // Examples
            string result = @this.ChangeExtension("cs");

            // Unit Test
            Assert.AreEqual(@thisNewFile.FullName, result);
        }
Ejemplo n.º 3
0
        private void Process(FileInfo file)
        {
            var input = string.IsNullOrWhiteSpace(Input)
                            ? file.Extension.Substring(1)
                            : Input;
            var destination = file.ChangeExtension(".{0}".FormatWith(Output));

            switch (input.ToUpperInvariant())
            {
            case "CSV":
                Process(new CsvDataSheet(file), destination);
                break;

            case "TSV":
                Process(new TsvDataSheet(file), destination);
                break;

            default:
                throw new FormatException("{0} is not a supported input data format.".FormatWith(input));
            }
        }
Ejemplo n.º 4
0
        public static void Run(string[] args)
        {
            #region Inputs Validation
            if (args.Length == 0)
            {
                Console.WriteLine(_SHORTHELP);
                return;
            }
            else if (args[0] == "-help")
            {
                Console.WriteLine(_LONGHELP);
                return;
            }

            var input = new FileInfo(args[0]);

            if (!input.Exists)
            {
                Console.WriteLine(_FILENOTFOUND, _INPUTFILE, input.FullName);
                return;
            }

            Console.WriteLine(_FILEFOUND, _INPUTFILE, input.FullName);
            #endregion

            #region Initial Parsing
            FileInfo output        = null;
            Format?  _inputFormat  = null;
            Format?  _outputFormat = null;

            var flags = new List <string>();
            int i     = 1;
            for (; i < args.Length; i++)
            {
                var arg = args[i];

                if (arg.StartsWith("-"))
                {
                    break;
                }

                // Read optional parameters that haven't been set yet in their specific order
                if (output == null && TryReadOutput(arg) ||
                    !_inputFormat.HasValue && TryReadInputFormat(arg) ||
                    !_outputFormat.HasValue && TryReadOutputFormat(arg))
                {
                    continue;
                }
            }
            #endregion

            #region Flags
            for (; i < args.Length; i++)
            {
                var arg = args[i];
                if (!arg.StartsWith("-"))
                {
                    Console.WriteLine(_INVALIDFLAG, arg);
                }
                flags.Add(arg.Substring(1).ToLowerInvariant());
            }

            if (flags.Count > 0)
            {
                Console.WriteLine();
            }
            foreach (string flag in flags)
            {
                switch (flag)
                {
                case "minify":
                    Minify = true;
                    Console.WriteLine(_FLAG, flag, _MINIFY);
                    break;

                case "noindent":
                    NoIndent = true;
                    Console.WriteLine(_FLAG, flag, _NOINDENT);
                    break;

                case "newlines":
                    NewLines = true;
                    Console.WriteLine(_FLAG, flag, _NEWLINES);
                    break;

                case "timer":
                    Timer = true;
                    Console.WriteLine(_FLAG, flag, _TIMER);
                    break;

                case "watch":
                    Watch = true;
                    Console.WriteLine(_FLAG, flag, _WATCH);
                    break;

                case "test":
                    Console.WriteLine(_FLAG, flag, _TEST);
                    Test = true;
                    break;

                default:
                    Console.WriteLine(_UNKNOWNFLAG, flag);
                    break;
                }
            }
            if (flags.Count > 0)
            {
                Console.WriteLine();
            }
            #endregion

            #region Input/Output
            // For some reason, VS can't see how outputFormat will always be assigned a value
            Format inputFormat, outputFormat = Format.XML;

            if (!_inputFormat.HasValue)
            {
                if (TryReadFormat(input.Extension.Substring(1), out Format format))
                {
                    Console.WriteLine(_INFERFORMAT, _INPUT, format.ToString());
                    inputFormat = format;
                }
                else
                {
                    Console.WriteLine(_DEFAUTFORMAT, _INPUT, _BIN);
                    inputFormat = Format.Binary;
                }
            }
            else
            {
                inputFormat = _inputFormat.Value;
            }

            // Test command only needs inputFormat, we need not decide other parameters and log them
            if (Test)
            {
                Console.WriteLine();

                // Watch bad
                Watch = false;

                var xml  = input.ChangeExtension(".xml");
                var json = input.ChangeExtension(".json");

                // Input -> XML
                Start(input, xml, inputFormat, Format.XML);
                // Input -> JSON
                Start(input, json, inputFormat, Format.JSON);

                // XML -> Binary
                Start(xml, xml.AddExtension(".bin"), Format.XML, Format.Binary);
                // JSON -> Binary
                Start(json, json.AddExtension(".bin"), Format.JSON, Format.Binary);

                return;
            }

            if (!_outputFormat.HasValue && output == null)
            {
                AutoOutputFormat();
                AutoOutput();
            }
            else if (!_outputFormat.HasValue)
            {
                if (TryReadFormat(output.Extension.Substring(1), out Format format))
                {
                    Console.WriteLine(_INFERFORMAT, _OUTPUT, format.ToString());
                    outputFormat = format;
                }
                else
                {
                    AutoOutputFormat();
                }
            }
            else if (output == null)
            {
                outputFormat = _outputFormat.Value;
                AutoOutput();
            }
            #endregion

            #region Serialization
            Console.WriteLine();
            Start(input, output, inputFormat, outputFormat);
            #endregion

            #region Utils
            void AutoOutput()
            {
                output = input.ChangeExtension(FormatToExt(outputFormat));
                Console.WriteLine(_AUTOFILE, output.FullName);
            }

            void AutoOutputFormat()
            {
                switch (inputFormat)
                {
                case Format.Binary:
                    Console.WriteLine(_DEFAUTFORMAT, _OUTPUT, _XML);
                    outputFormat = Format.XML;
                    break;

                default:
                    Console.WriteLine(_DEFAUTFORMAT, _OUTPUT, _BIN);
                    outputFormat = Format.Binary;
                    break;
                }
            }

            string FormatToExt(Format format)
            {
                switch (format)
                {
                case Format.Binary:
                    return(".bin");

                case Format.JSON:
                    return(".json");

                default:
                    return(".xml");
                }
            }

            #endregion

            #region Argument Parsers
            bool TryReadOutput(string arg)
            {
                var test = arg.ToLowerInvariant();

                // TODO: Figure out a better test
                if (test.Contains("."))
                {
                    output = new FileInfo(arg);
                    Console.WriteLine(_FILEFOUND, _OUTPUTFILE, output.FullName);
                    return(true);
                }

                return(false);
            }

            bool TryReadFormat(string arg, out Format format)
            {
                switch (arg.ToLowerInvariant())
                {
                case "xml":
                    format = Format.XML;
                    break;

                case "json":
                    format = Format.JSON;
                    break;

                default:
                    if (arg.StartsWith("bin"))
                    {
                        format = Format.Binary;
                    }
                    else
                    {
                        format = Format.XML;
                        return(false);
                    }
                    break;
                }

                return(true);
            }

            bool TryReadInputFormat(string arg)
            {
                if (TryReadFormat(arg, out Format format))
                {
                    _inputFormat = format;
                    Console.WriteLine(_FORMATFOUND, _INPUTFILE, _inputFormat.ToString());
                }

                return(false);
            }

            bool TryReadOutputFormat(string arg)
            {
                if (TryReadFormat(arg, out Format format))
                {
                    _inputFormat = format;
                    Console.WriteLine(_FORMATFOUND, _OUTPUTFILE, _inputFormat.ToString());
                }

                return(false);
            }

            #endregion
        }
Ejemplo n.º 5
0
        internal static Project Open(FileInfo projectFile)
        {
            var project = new Project
            {
                Settings = JsonSerialization.Deserialize <ProjectSettings>(projectFile.FullName)
            };

            project.Guid = new Guid(projectFile.ToAssetGuid());

            // Patch up any guid references if they are missing.
            if (Guid.Empty == project.Settings.Configuration)
            {
                project.Settings.Configuration = new Guid(AssetDatabaseUtility.GetAssetGuid(projectFile.ChangeExtension(k_ConfigurationFileExtension)));
            }

            if (Guid.Empty == project.Settings.MainAsmdef)
            {
                project.Settings.MainAsmdef = new Guid(AssetDatabaseUtility.GetAssetGuid(projectFile.ChangeExtension(k_AssemblyDefinitionFileExtension)));
            }

            if (!DomainReload.IsDomainReloading)
            {
                var entityManager = project.EntityManager;
                var configFile    = project.GetConfigurationFile();

                if (configFile.Exists)
                {
                    project.PersistenceManager.LoadScene(entityManager, configFile.FullName);

                    // Hack: remove scene guid/instance id
                    var configEntity = project.WorldManager.GetConfigEntity();
                    entityManager.RemoveComponent <SceneGuid>(configEntity);
                    entityManager.RemoveComponent <SceneInstanceId>(configEntity);
                }
                else
                {
                    var configEntity = project.WorldManager.CreateEntity(project.ArchetypeManager.Config);
                    entityManager.SetComponentData(configEntity, DisplayInfo.Default);
                }
            }

            s_Projects.Add(project);
            ProjectOpened(project);

            DomainCache.CacheIncludedAssemblies(project);
            return(project);
        }