CheckAndSetDefault() private method

return true if the operation succeeded. otherwise, return false
private CheckAndSetDefault ( ) : string
return string
Beispiel #1
0
        public override bool Execute()
        {
            _Options             = new LocBamlOptions();
            _Options.ToParse     = true;
            _Options.Input       = InputFile;
            _Options.Output      = OutputFile;
            _Options.CultureInfo = new CultureInfo(Culture);

            // Just to get rid of compiler warnings
            _Options.HasNoLogo = true;
            // generation-related options
            _Options.ToGenerate   = false;
            _Options.Translations = string.Empty;

            // TODO:
            if ((Assemblies == null) || (Assemblies.Length == 0))
            {
                _Options.AssemblyPaths = null;
            }
            else
            {
                _Options.AssemblyPaths = new System.Collections.ArrayList();
                _Options.AssemblyPaths.AddRange(Assemblies);
            }

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            // TODO: Add a logger interface so we can use MSBuild logger from
            // LocBamlOptions.Write() and WriteLine()
            _Options.IsVerbose = true;

            bool success = true;

            string errorString = _Options.CheckAndSetDefault();

            if (errorString != null)
            {
                Log.LogError(errorString);
                AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
                success = false;
            }
            else
            {
                try
                {
                    TranslationDictionariesWriter.Write(_Options);
                }
                catch (Exception e)
                {
                    Log.LogError("Exception parsing: {0}", e.Message);
                    success = false;
                }
                Log.LogMessage("Done with LocBamlParse");
            }

            // Cleanup
            AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;

            return(success);
        }
Beispiel #2
0
        /// <summary>
        /// get CommandLineOptions, return error message
        /// </summary>
        private static void GetCommandLineOptions(string[] args, out LocBamlOptions options, out string errorMessage)
        {
            CommandLine commandLine;
            try{
                // "*" means the option must have a value. no "*" means the option can't have a value
                 commandLine = new CommandLine(args,
                                    new string[]{
                                            "parse",        // /parse for update
                                            "generate",     // /generate     for generate
                                            "*out",         // /out          for output .csv|.txt when parsing, for output directory when generating
                                            "*culture",     // /culture      for culture name
                                            "*translation", // /translation  for translation file, .csv|.txt
                                            "*asmpath",     // /asmpath,     for assembly path to look for references   (TODO: add asmpath support)
                                            "nologo",       // /nologo       for not to print logo
                                            "help",         // /help         for help
                                            "verbose"       // /verbose      for verbose output
                                        }
                                     );
               }
               catch (ArgumentException e)
               {
               errorMessage = e.Message;
               options      = null;
               return;
               }

            if (commandLine.NumArgs + commandLine.NumOpts < 1)
            {
                PrintLogo(null);
                PrintUsage();
                errorMessage    = null;
                options         = null;
                return;
            }

            options = new LocBamlOptions();

            options.Input    = commandLine.GetNextArg();

            Option commandLineOption;

            while ( (commandLineOption = commandLine.GetNextOption()) != null)
            {
                if (commandLineOption.Name      == "parse")
                {
                    options.ToParse = true;
                }
                else if (commandLineOption.Name == "generate")
                {
                    options.ToGenerate = true;
                }
                else if (commandLineOption.Name == "nologo")
                {
                    options.HasNoLogo = true;
                }
                else if (commandLineOption.Name == "help")
                {
                    // we print usage and stop processing
                    PrintUsage();
                    errorMessage = null;
                    options = null;
                    return;
                }
                else if (commandLineOption.Name == "verbose")
                {
                    options.IsVerbose = true;
                }
                    // the following ones need value
                else if (commandLineOption.Name == "out")
                {
                    options.Output = commandLineOption.Value;
                }
                else if (commandLineOption.Name == "translation")
                {
                    options.Translations = commandLineOption.Value;
                }
                else if (commandLineOption.Name == "asmpath")
                {
                    if (options.AssemblyPaths == null)
                    {
                        options.AssemblyPaths = new ArrayList();
                    }

                    options.AssemblyPaths.Add(commandLineOption.Value);
                }
                else if (commandLineOption.Name == "culture")
                {
                    try
                    {
                        options.CultureInfo = new CultureInfo(commandLineOption.Value);
                    }
                    catch (ArgumentException e)
                    {
                        // Error
                        errorMessage = e.Message;
                        return;
                    }
                }
                else
                {
                    // something that we don't recognize
                    errorMessage = StringLoader.Get("Err_InvalidOption", commandLineOption.Name);
                    return;
                }
            }

            // we passed all the test till here. Now check the combinations of the options
            errorMessage = options.CheckAndSetDefault();
        }
Beispiel #3
0
        /// <summary>
        /// get CommandLineOptions, return error message
        /// </summary>
        private static void GetCommandLineOptions(string[] args, out LocBamlOptions options, out string errorMessage)
        {
            CommandLine commandLine;

            try{
                // "*" means the option must have a value. no "*" means the option can't have a value
                commandLine = new CommandLine(args,
                                              new string[] {
                    "parse",                                // /parse for update
                    "generate",                             // /generate     for generate
                    "*out",                                 // /out          for output .csv|.txt when parsing, for output directory when generating
                    "*culture",                             // /culture      for culture name
                    "*translation",                         // /translation  for translation file, .csv|.txt
                    "*asmpath",                             // /asmpath,     for assembly path to look for references   (TODO: add asmpath support)
                    "nologo",                               // /nologo       for not to print logo
                    "help",                                 // /help         for help
                    "verbose"                               // /verbose      for verbose output
                }
                                              );
            }
            catch (ArgumentException e)
            {
                errorMessage = e.Message;
                options      = null;
                return;
            }

            if (commandLine.NumArgs + commandLine.NumOpts < 1)
            {
                PrintLogo(null);
                PrintUsage();
                errorMessage = null;
                options      = null;
                return;
            }

            options = new LocBamlOptions();

            options.Input = commandLine.GetNextArg();

            Option commandLineOption;

            while ((commandLineOption = commandLine.GetNextOption()) != null)
            {
                if (commandLineOption.Name == "parse")
                {
                    options.ToParse = true;
                }
                else if (commandLineOption.Name == "generate")
                {
                    options.ToGenerate = true;
                }
                else if (commandLineOption.Name == "nologo")
                {
                    options.HasNoLogo = true;
                }
                else if (commandLineOption.Name == "help")
                {
                    // we print usage and stop processing
                    PrintUsage();
                    errorMessage = null;
                    options      = null;
                    return;
                }
                else if (commandLineOption.Name == "verbose")
                {
                    options.IsVerbose = true;
                }
                // the following ones need value
                else if (commandLineOption.Name == "out")
                {
                    options.Output = commandLineOption.Value;
                }
                else if (commandLineOption.Name == "translation")
                {
                    options.Translations = commandLineOption.Value;
                }
                else if (commandLineOption.Name == "asmpath")
                {
                    if (options.AssemblyPaths == null)
                    {
                        options.AssemblyPaths = new ArrayList();
                    }

                    options.AssemblyPaths.Add(commandLineOption.Value);
                }
                else if (commandLineOption.Name == "culture")
                {
                    try
                    {
                        options.CultureInfo = new CultureInfo(commandLineOption.Value);
                    }
                    catch (ArgumentException e)
                    {
                        // Error
                        errorMessage = e.Message;
                        return;
                    }
                }
                else
                {
                    // something that we don't recognize
                    errorMessage = StringLoader.Get("Err_InvalidOption", commandLineOption.Name);
                    return;
                }
            }

            // we passed all the test till here. Now check the combinations of the options
            errorMessage = options.CheckAndSetDefault();
        }
Beispiel #4
0
        public override bool Execute()
        {
            _Options             = new LocBamlOptions();
            _Options.CultureInfo = new CultureInfo(Culture);

            // generation-related options
            _Options.ToGenerate   = true;
            _Options.Translations = TranslationsFile;
            _Options.Input        = InputFile;
            _Options.Output       = OutputFolder;

            // TBD: Should we do this automatically or not?
            if (!System.IO.Directory.Exists(OutputFolder))
            {
                System.IO.Directory.CreateDirectory(OutputFolder);
            }

            _Options.ToParse = false;

            // Just to get rid of compiler warnings
            _Options.HasNoLogo = true;

            // TODO:
            if ((Assemblies == null) || (Assemblies.Length == 0))
            {
                _Options.AssemblyPaths = null;
            }
            else
            {
                _Options.AssemblyPaths = new System.Collections.ArrayList();
                _Options.AssemblyPaths.AddRange(Assemblies);
            }

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            // TODO: Add a logger interface so we can use MSBuild logger from
            // LocBamlOptions.Write() and WriteLine()
            _Options.IsVerbose = true;

            bool success = true;

            string errorString = _Options.CheckAndSetDefault();

            if (errorString != null)
            {
                Log.LogError(errorString);
                success = false;
            }
            else
            {
                try
                {
                    TranslationDictionariesReader dictionaries = _Options.GetTranslationsDictionary();
                    ResourceGenerator.Generate(_Options, dictionaries);
                }
                catch (Exception e)
                {
                    Log.LogError("Exception generating: {0}", e.Message);
                    success = false;
                }
                Log.LogMessage("Done with LocBamlGenerate");
            }
            AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            return(success);
        }
Beispiel #5
0
        public override bool Execute()
        {
            // First stage: parse
            _Options             = new LocBamlOptions();
            _Options.ToParse     = true;
            _Options.Input       = InputFile;
            _Options.Output      = TranslationsFile;
            _Options.CultureInfo = new CultureInfo(Culture);

            // Just to get rid of compiler warnings
            _Options.HasNoLogo = true;
            // generation-related options
            _Options.ToGenerate   = false;
            _Options.Translations = string.Empty;

            // TODO:
            if ((Assemblies == null) || (Assemblies.Length == 0))
            {
                _Options.AssemblyPaths = null;
            }
            else
            {
                _Options.AssemblyPaths = new System.Collections.ArrayList();
                _Options.AssemblyPaths.AddRange(Assemblies);
            }

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            // TODO: Add a logger interface so we can use MSBuild logger from
            // LocBamlOptions.Write() and WriteLine()
            _Options.IsVerbose = true;

            bool success = true;

            string errorString = _Options.CheckAndSetDefault();

            if (errorString != null)
            {
                Log.LogError(errorString);
                AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
                success = false;
            }
            else
            {
                try
                {
                    TranslationDictionariesWriter.Write(_Options);
                }
                catch (Exception e)
                {
                    Log.LogError("Exception parsing: {0}", e.Message);
                    success = false;
                }
            }

            // TBD: a way to override automatic algorithm?
            string outputFolder = Path.GetDirectoryName(Path.GetFullPath(InputFile));

            string[] folderParts = outputFolder.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            // Try to figure out if the ouptut folder is a culture name, if so remove it
            foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
            {
                string cName = culture.Name;
                if (string.IsNullOrEmpty(cName))
                {
                    continue;
                }
                if (folderParts[folderParts.Length - 1].Equals(cName, StringComparison.InvariantCultureIgnoreCase))
                {
                    outputFolder = string.Join(Path.DirectorySeparatorChar.ToString(), folderParts, 0, folderParts.Length - 1);
                    outputFolder = Path.Combine(outputFolder, _Options.CultureInfo.Name);
                    break;
                }
            }

            if (success)
            {
                // Don't call _Options.CheckAndSetDefault() again because it will reload the assemblies
                // generation-related options
                _Options.ToGenerate   = true;
                _Options.Translations = TranslationsFile;
                _Options.Input        = InputFile;
                _Options.Output       = outputFolder;

                // TBD: Should we do this automatically or not?
                if (!Directory.Exists(outputFolder))
                {
                    Directory.CreateDirectory(outputFolder);
                }

                _Options.ToParse = false;

                try
                {
                    TranslationDictionariesReader dictionaries = _Options.GetTranslationsDictionary();
                    ResourceGenerator.Generate(_Options, dictionaries);
                }
                catch (Exception e)
                {
                    Log.LogError("Exception generating: {0}", e.Message);
                    success = false;
                }
            }

            // Cleanup
            AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;

            return(success);
        }