/// <summary>
        /// Entry point when called from MSBuild.
        /// </summary>
        /// <returns>True if successfully executed.</returns>
        public override bool Execute()
        {
            Messaging messaging = new Messaging(this);

            return(this.Run(messaging));
        }
        private bool Run(Messaging messaging)
        {
            if (this.SourcePaths.Length == 0)
            {
                messaging.OnError(null, "No inputs specified. Specify at least one file.");
            }

            if (String.IsNullOrEmpty(this.Type))
            {
                if (this.OutputPath == null)
                {
                    messaging.OnError(this, "Package type cannot be inferred from output path. Explicitly specify PackageType in your MSBuild project or -type from the swc.exe command-line. Valid options are: appx, msi, nuget, vsix, or wixlib");
                }
                else
                {
                    this.Type = Path.GetExtension(this.OutputPath.ItemSpec);
                }
            }
            else if (this.OutputPath == null && this.SourcePaths.Length > 0)
            {
                string outputPath = Path.ChangeExtension(this.SourcePaths[0].ItemSpec, this.Type.ToLowerInvariant());
                this.OutputPath = new FilePathTaskItem(outputPath);
            }

            PackageArchitecture architecture = PackageArchitecture.Unknown;

            if (String.IsNullOrEmpty(this.Architecture))
            {
                messaging.OnError(this, "A package architecture must specified. Set the PackageArchitecture in your MSBuild project or -arch from the swc.exe command-line. Valid options are: arm, x64, x86 or neutral");
            }
            else if (!SimplifiedWixCompiler.TryConvertPackageArchitecture(this.Architecture, out architecture))
            {
                messaging.OnError(this, "Unknown architecture specified: {0}. Valid options are: arm, x64, x86 or neutral", this.Architecture);
            }

            List <CultureInfo> locales = new List <CultureInfo>();

            if (this.Languages != null)
            {
                foreach (string language in this.Languages)
                {
                    try
                    {
                        CultureInfo locale;

                        int lcid = 0;
                        if (Int32.TryParse(language, out lcid))
                        {
                            locale = new CultureInfo(lcid);
                        }
                        else
                        {
                            locale = new CultureInfo(language);
                        }

                        locales.Add(locale);
                    }
                    catch (CultureNotFoundException)
                    {
                        messaging.OnError(this, "Unknown language: {0}", language);
                    }
                }
            }

            if (String.IsNullOrEmpty(this.Type))
            {
                messaging.OnError(this, "A package type must specified. Use the PackageType in your MSBuild project or -type from the swc.exe command-line. Valid options are: appx, msi, nuget, vsix or wixlib");
            }

            if (!messaging.Errored)
            {
                SimplifiedCompiler compiler = new SimplifiedCompiler();
                compiler.Messages += messaging.MessageDelegate;

                if (this.SearchPaths != null)
                {
                    foreach (ITaskItem searchPath in this.SearchPaths)
                    {
                        compiler.SearchPaths.Add(searchPath.ItemSpec);
                    }
                }

                if (this.PreprocessorDefines != null)
                {
                    foreach (string define in this.PreprocessorDefines)
                    {
                        string[] defineSplit = define.Split(new char[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries);
                        compiler.PreprocessorDefines.Add(defineSplit[0], defineSplit.Length > 1 ? defineSplit[1] : null);
                    }
                }

                // Load the extensions.
                if (this.Extensions != null)
                {
                    SimplifiedWixCompiler.LoadExtensions(compiler, messaging, this.Extensions);
                }

                CompilerExtension outputExtension = compiler.Extensions.FirstOrDefault(e => e.HasBackendCompiler(this.Type));
                if (outputExtension != null)
                {
                    if (!messaging.Errored)
                    {
                        SimplifiedWixCompiler.LoadPackageSources(compiler, this.SourcePaths);
                        compiler.Compile(outputExtension, architecture, locales.ToArray(), this.OutputPath.ItemSpec);
                    }
                }
                else
                {
                    PackageType type = PackageType.Unknown;
                    if (!SimplifiedWixCompiler.TryConvertPackageType(this.Type, out type))
                    {
                        messaging.OnError(this, "Unknown package type specified: {0}. Valid options are: appx, msi, nuget, vsix or wixlib", this.Type);
                    }

                    if (type == PackageType.Appx && locales.Count == 0)
                    {
                        messaging.OnError(this, "AppX packages do not support language neutral packages. At least one language to be specified. Use the PackageLanguages property in your MSBuild project or -lang from the swc.exe command-line.");
                    }

                    // Finally, load the sources and compile!
                    if (!messaging.Errored)
                    {
                        SimplifiedWixCompiler.LoadPackageSources(compiler, this.SourcePaths);
                        compiler.Compile(type, architecture, locales.ToArray(), this.OutputPath.ItemSpec);
                    }
                }
            }

            return(!messaging.Errored);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses the command-line.
        /// </summary>
        /// <param name="args">Arguments from command-line.</param>
        /// <param name="messaging">Messaging object to send errors.</param>
        /// <param name="commandLine">Command line object created from command-line arguments</param>
        /// <returns>True if command-line is parsed, false if a failure was occurred.</returns>
        public static bool TryParseArguments(string[] args, Messaging messaging, out CommandLine commandLine)
        {
            bool success = true;

            commandLine = new CommandLine();

            for (int i = 0; i < args.Length; ++i)
            {
                if ('-' == args[i][0] || '/' == args[i][0])
                {
                    string arg = args[i].Substring(1).ToLowerInvariant();
                    if ("?" == arg || "help" == arg)
                    {
                        commandLine.Help = true;
                    }
                    else if ("arch" == arg || "architecture" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing architecture specification for '-architecture' option. Provide one of the following: x64, x86 or neutral.");
                            success = false;
                        }
                        else
                        {
                            commandLine.Architecture = args[i];
                        }
                    }
                    else if ("certificate" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing file specification for '-certificate' option.");
                            success = false;
                        }
                        else
                        {
                            //SimplifiedWixCompiler.certificatePath = Path.GetFullPath(args[i]);
                        }
                    }
                    else if ("d" == arg || "define" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing preprocessor definition for '-define' option. Provide a preprocessor definition in the form of: name or name=variable.");
                            success = false;
                        }
                        else
                        {
                            commandLine.PreprocessorDefines.Add(args[i]);
                        }
                    }
                    else if ("ext" == arg || "extension" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing reference specification for '-extension' option.");
                            success = false;
                        }
                        else
                        {
                            string[]         extensionData = args[i].Split(new string[] { ";" }, 2, StringSplitOptions.RemoveEmptyEntries);
                            FilePathTaskItem extension     = new FilePathTaskItem(extensionData[0]);
                            if (extensionData.Length > 1)
                            {
                                extension.SetMetadata("Data", extensionData[1]);
                            }

                            commandLine.Extensions.Add(extension);
                        }
                    }
                    else if ("lang" == arg || "language" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing file specification for '-language' option. Provide a valid language identifier such as: 1033 or en-US.");
                            success = false;
                        }
                        else
                        {
                            string[] languages = args[i].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                            commandLine.Languages.AddRange(languages);
                        }
                    }
                    else if ("nologo" == arg)
                    {
                        commandLine.NoLogo = true;
                    }
                    else if ("o" == arg || "out" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing file specification for '-out' option.");
                            success = false;
                        }
                        else
                        {
                            string outputPath = Path.GetFullPath(args[i]);
                            commandLine.Output = new FilePathTaskItem(outputPath);
                        }
                    }
                    else if ("r" == arg || "reference" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing reference specification for '-reference' option.");
                            success = false;
                        }
                        else
                        {
                            commandLine.References.Add(new FilePathTaskItem(args[i]));
                        }
                    }
                    else if ("sp" == arg || "searchpath" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing reference specification for '-searchpath' option.");
                            success = false;
                        }
                        else
                        {
                            commandLine.SearchPaths.Add(new FilePathTaskItem(args[i]));
                        }
                    }
                    else if ("t" == arg || "type" == arg)
                    {
                        ++i;
                        if (args.Length == i)
                        {
                            messaging.OnError(null, "Missing type specification for '-type' option. Specify appx, msi, or wixlib.");
                            success = false;
                        }
                        else
                        {
                            commandLine.Type = args[i];
                        }
                    }
                    else if ("v" == arg || "verbose" == arg)
                    {
                        commandLine.Verbose = true;
                        messaging.Verbose   = true;
                    }
                }
                else
                {
                    string[] file       = args[i].Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    string   sourcePath = Path.GetFullPath(file[0]);
                    if (!System.IO.File.Exists(sourcePath))
                    {
                        messaging.OnError(null, "Source file '{0}' could not be found.", sourcePath);
                        success = false;
                    }
                    else
                    {
                        FilePathTaskItem item = new FilePathTaskItem(sourcePath);
                        if (file.Length > 1)
                        {
                            item.SetMetadata("TargetPath", file[1]);
                        }

                        commandLine.Files.Add(item);
                    }
                }
            }

            return(success);
        }