private static void LoadExtensions(SimplifiedCompiler compiler, Messaging messaging, ITaskItem[] extensions)
        {
            foreach (ITaskItem extension in extensions)
            {
                string className = extension.GetMetadata("Class");
                string data      = extension.GetMetadata("Data");

                // First, try the HintPath.
                string resolvedPath = extension.GetMetadata("HintPath");
                if (String.IsNullOrEmpty(resolvedPath) || !File.Exists(resolvedPath))
                {
                    // Try the item as a DLL.
                    resolvedPath = extension.ItemSpec;
                    if (Path.GetExtension(resolvedPath).Length == 0)
                    {
                        resolvedPath += ".dll";
                    }

                    if (!File.Exists(resolvedPath))
                    {
                        // Finally a file on disk wasn't found, so just set it to the extension name passed in.
                        resolvedPath = extension.ItemSpec;
                    }
                }

                try
                {
                    string extensionName = resolvedPath;
                    if (!String.IsNullOrEmpty(className))
                    {
                        extensionName = String.Concat(className, ", ", extensionName);
                    }

                    // Load the extension and feed it its arguments if any were provided.
                    CompilerExtension compilerExtension = CompilerExtension.Load(extensionName);
                    compilerExtension.Messages += messaging.MessageDelegate;
                    compilerExtension.Data      = data;

                    // If the compiler file manager hasn't been set yet, set it to the extension's file manager.
                    if (compiler.FileManager == null)
                    {
                        compiler.FileManager = compilerExtension.FileManager;
                    }

                    compiler.AddExtension(compilerExtension);
                }
                catch (CompilerException e)
                {
                    messaging.OnError(null, "{1}", e.Message);
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Compiles the added files using the given CompilerExtension to generate the output
 /// </summary>
 /// <param name="outputExtension">Extension used to generated the output.</param>
 /// <param name="architecture">Package architecture to generate.</param>
 /// <param name="languages">Package languages to generate.</param>
 /// <param name="outputPath">Path to generate output.</param>
 public void Compile(CompilerExtension outputExtension, PackageArchitecture architecture, CultureInfo[] languages, string outputPath)
 {
     CoreCompile(architecture, languages, outputPath, () => outputExtension.CreateBackendCompiler());
 }
        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);
        }
コード例 #4
0
 /// <summary>
 /// Adds an extension that will be used during the compile
 /// </summary>
 /// <param name="extension">The extension to be added</param>
 public void AddExtension(CompilerExtension extension)
 {
     this.extensions.Add(extension);
 }