Example #1
0
        public static async Task <(bool wasAlreadyPresent, string localPath)> FetchArtifact(
            BuildContext ctx, ArtifactPackage ap, bool forceSwitch)
        {
            var destDir = Path.Combine(ctx.InDir, Path.GetFileNameWithoutExtension(ap.FileName));

            var localPath = Path.Combine(destDir, ap.FileName);

            if (!forceSwitch && File.Exists(localPath))
            {
                return(true, localPath);
            }

            if (!ap.IsDownloadable)
            {
                throw new Exception($"{ap.FileName} is missing {nameof(ap.Url)}");
            }



            localPath = Path.Combine(destDir, Path.GetFileName(ap.Url));

            Directory.CreateDirectory(destDir);

            using var http = new HttpClient();
            using var stm  = await http.GetStreamAsync(ap.Url);

            using var fs = File.Open(localPath, FileMode.Create, FileAccess.Write);

            // Buffer size just shy of one that would get onto LOH
            // (hopefully ArrayPool will oblige...)

            var bytes = ArrayPool <byte> .Shared.Rent(81920);

            try
            {
                int bytesRead = 0;

                while (true)
                {
                    if ((bytesRead = await stm.ReadAsync(bytes, 0, bytes.Length)) <= 0)
                    {
                        break;
                    }

                    await fs.WriteAsync(bytes, 0, bytesRead);
                }
            }
            finally
            {
                if (bytes != null)
                {
                    ArrayPool <byte> .Shared.Return(bytes);
                }
            }

            return(false, localPath);
        }
Example #2
0
        public static async Task <IEnumerable <ArtifactPackage> > FindArtifact(
            string target, Action <ArtifactFilter> filterConfiguration)
        {
            // TODO: validate filterConfiguraion

            var filter = new ArtifactFilter(target);

            filterConfiguration?.Invoke(filter);

            using var http = new HttpClient()
                  {
                      BaseAddress = BaseAddress,
                      Timeout     = TimeSpan.FromMilliseconds(3000)
                  };

            var query = $"search/{filter.ContainerId}/{target}{filter.QueryString}";

            using var stm = await http.GetStreamAsync(query);

            using var sr  = new StreamReader(stm);
            using var jtr = new JsonTextReader(sr);

            var js   = new JsonSerializer();
            var data = js.Deserialize <JToken>(jtr);

            var packages = new List <ArtifactPackage>();

            foreach (JProperty itm in data["packages"] ?? new JArray())
            {
                if (filter.Bitness == eBitness.x64 &&
                    (string)itm.Value[MagicStrings.ArtifactsApi.Architecture] != MagicStrings.Arch.x86_64)
                {
                    continue;
                }

                var packageUrl = (string)itm.Value[MagicStrings.ArtifactsApi.Url];
                if (packageUrl.IsEmpty())
                {
                    continue;
                }

                if (!ArtifactPackage.FromUrl(packageUrl, out var package))
                {
                    continue;
                }

                packages.Add(package);
            }

            return(packages);
        }
 public void Write(TProtocol oprot)
 {
     oprot.IncrementRecursionDepth();
     try
     {
         TStruct struc = new TStruct("TDDIArtifactPackageUnion");
         oprot.WriteStructBegin(struc);
         TField field = new TField();
         if (ArtifactPackage != null && __isset.ArtifactPackage)
         {
             field.Name = "ArtifactPackage";
             field.Type = TType.Struct;
             field.ID   = 1;
             oprot.WriteFieldBegin(field);
             ArtifactPackage.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (ArtifactPackageInterface != null && __isset.ArtifactPackageInterface)
         {
             field.Name = "ArtifactPackageInterface";
             field.Type = TType.Struct;
             field.ID   = 2;
             oprot.WriteFieldBegin(field);
             ArtifactPackageInterface.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (ArtifactPackageBinding != null && __isset.ArtifactPackageBinding)
         {
             field.Name = "ArtifactPackageBinding";
             field.Type = TType.Struct;
             field.ID   = 3;
             oprot.WriteFieldBegin(field);
             ArtifactPackageBinding.Write(oprot);
             oprot.WriteFieldEnd();
         }
         oprot.WriteFieldStop();
         oprot.WriteStructEnd();
     }
     finally
     {
         oprot.DecrementRecursionDepth();
     }
 }
Example #4
0
        public static async Task <IEnumerable <ArtifactPackage> > DiscoverArtifacts(string target, string containerId)
        {
            using var http = new HttpClient()
                  {
                      BaseAddress = BaseAddress,
                      Timeout     = TimeSpan.FromMilliseconds(3000)
                  };

            var query = $"search/{containerId}/{target},windows,zip";

            using var stm = await http.GetStreamAsync(query);

            using var sr  = new StreamReader(stm);
            using var jtr = new JsonTextReader(sr);

            var js   = new JsonSerializer();
            var data = js.Deserialize <JToken>(jtr);

            var packages = new List <ArtifactPackage>();

            foreach (JProperty itm in data["packages"] ?? new JArray())
            {
                var packageUrl = (string)itm.Value[MagicStrings.ArtifactsApi.Url];
                if (packageUrl.IsEmpty())
                {
                    continue;
                }

                if (!ArtifactPackage.FromUrl(packageUrl, out var package))
                {
                    continue;
                }

                packages.Add(package);
            }

            return(packages);
        }
Example #5
0
 public static void SetArtifactPackage(this BuildContext ctx, ArtifactPackage ap) =>
 ctx.Items.Add(nameof(ArtifactPackage), ap);
        static void Main(string[] args)
        {
            var opts = CmdLineOptions.Parse(args);

            var config = BuildConfiguration.Read(
                Path.Combine(opts.ConfigDir, MagicStrings.Files.ConfigYaml));

            Directory.CreateDirectory(opts.PackageOutDir);

            var ap = new ArtifactPackage()
            {
                TargetName          = "lsbeat",
                CanonicalTargetName = "lsbeat",
                Architecture        = MagicStrings.Arch.x86,
                Version             = Environment.GetEnvironmentVariable("GITHUB_VERSION").Trim('v'),
            };

            Console.WriteLine(ap.ToString());

            var pc = config.GetProductConfig(ap.TargetName);

            var companyName    = "Mirero";
            var productSetName = MagicStrings.Beats.Name;
            var displayName    = companyName + " " + MagicStrings.Beats.Name + " " + ap.TargetName;
            var exeName        = ap.CanonicalTargetName + MagicStrings.Ext.DotExe;

            // Generate UUID v5 from product properties.
            // This UUID *must* be stable and unique between Beats.
            var upgradeCode = Uuid5.FromString(ap.CanonicalTargetName);

            var project = new Project(displayName)
            {
                InstallerVersion = 500,

                GUID = upgradeCode,

                Name = $"{displayName} {ap.SemVer} ({ap.Architecture})",

                Description = pc.Description,

                OutFileName = Path.Combine(opts.PackageOutDir, opts.ShortPackageName),
                Version     = new Version(ap.Version),

                Platform = ap.Is32Bit ? Platform.x86 : Platform.x64,

                InstallScope = InstallScope.perMachine,

                UI = WUI.WixUI_Minimal,

                // TODO: Custom images?
                BannerImage     = Path.Combine(opts.ResDir, MagicStrings.Files.TopBannerBmp),
                BackgroundImage = Path.Combine(opts.ResDir, MagicStrings.Files.LeftBannerBmp),

                MajorUpgrade = new MajorUpgrade
                {
                    AllowDowngrades          = false,
                    AllowSameVersionUpgrades = false,
                    DowngradeErrorMessage    = MagicStrings.Errors.NewerVersionInstalled,
                },
            };

            project.Include(WixExtension.UI);
            project.Include(WixExtension.Util);

            project.ControlPanelInfo = new ProductInfo
            {
                Contact      = companyName,
                Manufacturer = companyName,
                UrlInfoAbout = "https://www.mirero.co.kr",

                Comments = pc.Description + ". " + MagicStrings.Beats.Description,

                ProductIcon = Path.Combine(
                    opts.ResDir,
                    Path.GetFileNameWithoutExtension(exeName) + MagicStrings.Ext.DotIco),

                NoRepair = true,
            };


            var beatConfigPath = "[CommonAppDataFolder]" + Path.Combine(companyName, productSetName, ap.CanonicalTargetName);
            var beatDataPath   = Path.Combine(beatConfigPath, "data");
            var beatLogsPath   = Path.Combine(beatConfigPath, "logs");

            var textInfo           = new CultureInfo("en-US", false).TextInfo;
            var serviceDisplayName = $"{companyName} {textInfo.ToTitleCase(ap.TargetName)} {ap.SemVer}";

            WixSharp.File service = null;
            if (pc.IsWindowsService)
            {
                service = new WixSharp.File(Path.Combine(opts.PackageInDir, exeName));

                // TODO: CNDL1150 : ServiceConfig functionality is documented in the Windows Installer SDK to
                //                  "not [work] as expected." Consider replacing ServiceConfig with the
                //                  WixUtilExtension ServiceConfig element.

                service.ServiceInstaller = new ServiceInstaller
                {
                    Interactive = false,

                    Name        = ap.CanonicalTargetName,
                    DisplayName = serviceDisplayName,
                    Description = pc.Description,

                    DependsOn = new[]
                    {
                        new ServiceDependency(MagicStrings.Services.Tcpip),
                        new ServiceDependency(MagicStrings.Services.Dnscache),
                    },

                    Arguments =
                        " --path.home " + ("[INSTALLDIR]" + Path.Combine(ap.Version, ap.CanonicalTargetName)).Quote() +
                        " --path.config " + beatConfigPath.Quote() +
                        " --path.data " + beatDataPath.Quote() +
                        " --path.logs " + beatLogsPath.Quote() +
                        " -E logging.files.redirect_stderr=true",

                    DelayedAutoStart = false,
                    Start            = SvcStartType.auto,

                    StartOn  = SvcEvent.Install,
                    StopOn   = SvcEvent.InstallUninstall_Wait,
                    RemoveOn = SvcEvent.InstallUninstall_Wait,
                };
            }

            var packageContents = new List <WixEntity>
            {
                new DirFiles(Path.Combine(opts.PackageInDir, MagicStrings.Files.All), path =>
                {
                    var itm = path.ToLower();

                    bool exclude =

                        // configuration will go into mutable location
                        itm.EndsWith(MagicStrings.Ext.DotYml, StringComparison.OrdinalIgnoreCase) ||

                        // we install/remove service ourselves
                        itm.EndsWith(MagicStrings.Ext.DotPs1, StringComparison.OrdinalIgnoreCase) ||

                        itm.EndsWith(MagicStrings.Ext.DotTxt, StringComparison.OrdinalIgnoreCase) ||

                        itm.EndsWith(MagicStrings.Ext.DotMd, StringComparison.OrdinalIgnoreCase) ||

                        // .exe must be excluded for service configuration to work
                        (pc.IsWindowsService && itm.EndsWith(exeName, StringComparison.OrdinalIgnoreCase))
                    ;

                    // this is an "include" filter
                    return(!exclude);
                })
            };

            packageContents.AddRange(
                new DirectoryInfo(opts.PackageInDir)
                .GetDirectories()
                .Select(dir => dir.Name)
                .Except(pc.MutableDirs)
                .Select(dirName =>
                        new Dir(
                            dirName,
                            new Files(Path.Combine(
                                          opts.PackageInDir,
                                          dirName,
                                          MagicStrings.Files.All)))));

            packageContents.Add(pc.IsWindowsService ? service : null);
            project.AddProperty(new Property("WIXUI_EXITDIALOGOPTIONALTEXT",
                                             $"NOTE: {serviceDisplayName} Windows service.\n"));

            var dataContents = new List <WixEntity>();
            var extraDir     = Path.Combine(opts.ExtraDir, ap.TargetName);

            dataContents.AddRange(
                new DirectoryInfo(extraDir)
                .GetFiles(MagicStrings.Files.AllDotYml, SearchOption.TopDirectoryOnly)
                .Select(fi =>
            {
                var wf = new WixSharp.File(fi.FullName);
                return(wf);
            }));

            dataContents.AddRange(
                new DirectoryInfo(extraDir)
                .GetDirectories()
                .Select(dir => dir.Name)
                .Select(dirName =>
                        new Dir(
                            dirName,
                            new Files(Path.Combine(
                                          extraDir,
                                          dirName,
                                          MagicStrings.Files.All)))));

            // Drop CLI shim on disk
            var cliShimScriptPath = Path.Combine(
                opts.PackageOutDir,
                MagicStrings.Files.ProductCliShim(ap.CanonicalTargetName));

            System.IO.File.WriteAllText(cliShimScriptPath, Resources.GenericCliShim);

            var beatsInstallPath =
                $"[ProgramFiles{(ap.Is64Bit ? "64" : string.Empty)}Folder]" +
                Path.Combine(companyName, productSetName);

            project.Dirs = new[]
            {
                // Binaries
                new InstallDir(
                    // Wix# directory parsing needs forward slash
                    beatsInstallPath.Replace("Folder]", "Folder]\\"),
                    new Dir(
                        ap.Version,
                        new Dir(ap.CanonicalTargetName, packageContents.ToArray()),
                        new WixSharp.File(cliShimScriptPath))),

                // Configration and logs
                new Dir("[CommonAppDataFolder]",
                        new Dir(companyName,
                                new Dir(productSetName,
                                        new Dir(ap.CanonicalTargetName, dataContents.ToArray())
                                        , new DirPermission("Users", "[MachineName]", GenericPermission.All)
                                        )))
            };

            // CLI Shim path
            project.Add(new EnvironmentVariable("PATH", Path.Combine(beatsInstallPath, ap.Version))
            {
                Part = EnvVarPart.last
            });

            // We hard-link Wix Toolset to a known location
            Compiler.WixLocation = Path.Combine(opts.BinDir, "WixToolset", "bin");

#if !DEBUG
            if (opts.KeepTempFiles)
#endif
            {
                Compiler.PreserveTempFiles = true;
            }

            if (opts.Verbose)
            {
                Compiler.CandleOptions += " -v";
                Compiler.LightOptions  += " -v";
            }

            project.ResolveWildCards();

            if (opts.WxsOnly)
            {
                project.BuildWxs();
            }
            else if (opts.CmdOnly)
            {
                Compiler.BuildMsiCmd(project, Path.Combine(opts.SrcDir, opts.PackageName) + ".cmd");
            }
            else
            {
                Compiler.BuildMsi(project);
            }
        }
        public static async Task RunAsync(BuildContext ctx, string targetName)
        {
            var  cmd         = ctx.GetCommand();
            bool forceSwitch = true;

            //bool forceSwitch = (cmd as ISupportForceSwitch)?.ForceSwitch ?? false;

            if (!forceSwitch && !ctx.Config.ProductNames.Any(t => t.ToLower() == targetName))
            {
                throw new InvalidDataException($"Invalid product '{targetName}'");
            }

            var bitness     = (cmd as ISupportBitnessChoice).Bitness;
            var containerId = (cmd as ISupportRequiredContainerId).ContainerId;

            if (!QualifiedVersion.FromString(containerId, out var qv))
            {
                throw new InvalidDataException($"Bad container id: '{containerId}'");
            }

            Directory.CreateDirectory(ctx.InDir);

            if (!forceSwitch)
            {
                await Console.Out.WriteLineAsync($"Searching local directory {ctx.InDir} ...");
            }

            var packageList = forceSwitch

                              // When --force in effect, we always lookup from artifacts-api
                ? new List <ArtifactPackage>()

                              // Lookup in the bin/in first
                : new DirectoryInfo(ctx.InDir)
                              .GetFiles(MagicStrings.Files.AllDotZip, SearchOption.TopDirectoryOnly)
                              .Select(fi =>
            {
                if (!ArtifactPackage.FromFilename(fi.Name, out var ap))
                {
                    return(null);
                }

                if (ap.TargetName != targetName)
                {
                    return(null);
                }

                if (ap.SemVer != qv.SemVer)
                {
                    return(null);
                }

                if (!fi.Name.Contains("-windows", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }

                return(ap);
            })
                              .Where(ap =>
            {
                return((ap != null) && bitness switch
                {
                    eBitness.x86 => ap.Is32Bit,
                    eBitness.x64 => ap.Is64Bit,
                    eBitness.both => true,
                    _ => false
                });
            })
Example #8
0
        static void Main(string[] args)
        {
            var opts = CmdLineOptions.Parse(args);

            var config = BuildConfiguration.Read(
                Path.Combine(opts.ConfigDir, MagicStrings.Files.ConfigYaml));

            Directory.CreateDirectory(opts.PackageOutDir);

            if (!ArtifactPackage.FromFilename(opts.PackageName, out var ap))
            {
                throw new Exception("Unable to parse file name: " + opts.PackageName);
            }

            var pc = config.GetProductConfig(ap.TargetName);

            var companyName    = MagicStrings.Elastic;
            var productSetName = MagicStrings.Beats.Name;
            var displayName    = MagicStrings.Beats.Name + " " + ap.TargetName;
            var exeName        = ap.CanonicalTargetName + MagicStrings.Ext.DotExe;

            // Generate UUID v5 from product properties.
            // This UUID *must* be stable and unique between Beats.
            var upgradeCode = Uuid5.FromString(ap.CanonicalTargetName);

            var project = new Project(displayName)
            {
                InstallerVersion = 500,

                GUID = upgradeCode,

                Name = $"{displayName} {ap.SemVer} ({ap.Architecture})",

                Description = pc.Description,

                OutFileName = Path.Combine(opts.PackageOutDir, opts.PackageName),
                Version     = new Version(ap.Version),

                // We massage LICENSE.txt into .rtf below
                LicenceFile = Path.Combine(
                    opts.PackageOutDir,
                    MagicStrings.Files.PackageLicenseRtf(opts.PackageName)),

                Platform = ap.Is32Bit ? Platform.x86 : Platform.x64,

                InstallScope = InstallScope.perMachine,

                UI = WUI.WixUI_Minimal,

                // TODO: Custom images?
                BannerImage     = Path.Combine(opts.ResDir, MagicStrings.Files.TopBannerBmp),
                BackgroundImage = Path.Combine(opts.ResDir, MagicStrings.Files.LeftBannerBmp),

                MajorUpgrade = new MajorUpgrade
                {
                    AllowDowngrades          = false,
                    AllowSameVersionUpgrades = false,
                    DowngradeErrorMessage    = MagicStrings.Errors.NewerVersionInstalled,
                },
            };

            project.Include(WixExtension.UI);
            project.Include(WixExtension.Util);

            project.ControlPanelInfo = new ProductInfo
            {
                Contact      = companyName,
                Manufacturer = companyName,
                UrlInfoAbout = "https://www.elastic.co",

                Comments = pc.Description + ". " + MagicStrings.Beats.Description,

                ProductIcon = Path.Combine(
                    opts.ResDir,
                    Path.GetFileNameWithoutExtension(exeName) + MagicStrings.Ext.DotIco),

                NoRepair = true,
            };

            // Convert LICENSE.txt to something richedit control can render
            System.IO.File.WriteAllText(
                Path.Combine(
                    opts.PackageOutDir,
                    MagicStrings.Files.PackageLicenseRtf(opts.PackageName)),
                MagicStrings.Content.WrapWithRtf(
                    System.IO.File.ReadAllText(
                        Path.Combine(opts.PackageInDir, MagicStrings.Files.LicenseTxt))));

            var beatConfigPath = "[CommonAppDataFolder]" + Path.Combine(companyName, productSetName, ap.CanonicalTargetName);
            var beatDataPath   = Path.Combine(beatConfigPath, "data");
            var beatLogsPath   = Path.Combine(beatConfigPath, "logs");

            var textInfo           = new CultureInfo("en-US", false).TextInfo;
            var serviceDisplayName = $"{companyName} {textInfo.ToTitleCase(ap.TargetName)} {ap.SemVer}";

            WixSharp.File service = null;
            if (pc.IsWindowsService)
            {
                service = new WixSharp.File(Path.Combine(opts.PackageInDir, exeName));

                // TODO: CNDL1150 : ServiceConfig functionality is documented in the Windows Installer SDK to
                //                  "not [work] as expected." Consider replacing ServiceConfig with the
                //                  WixUtilExtension ServiceConfig element.

                service.ServiceInstaller = new ServiceInstaller
                {
                    Interactive = false,

                    Name        = ap.CanonicalTargetName,
                    DisplayName = serviceDisplayName,
                    Description = pc.Description,

                    DependsOn = new[]
                    {
                        new ServiceDependency(MagicStrings.Services.Tcpip),
                        new ServiceDependency(MagicStrings.Services.Dnscache),
                    },

                    Arguments =
                        " --path.home " + ("[INSTALLDIR]" + Path.Combine(ap.Version, ap.CanonicalTargetName)).Quote() +
                        " --path.config " + beatConfigPath.Quote() +
                        " --path.data " + beatDataPath.Quote() +
                        " --path.logs " + beatLogsPath.Quote() +
                        " -E logging.files.redirect_stderr=true",

                    DelayedAutoStart = false,
                    Start            = SvcStartType.auto,

                    // Don't start on install, config file is likely not ready yet
                    //StartOn = SvcEvent.Install,

                    StopOn   = SvcEvent.InstallUninstall_Wait,
                    RemoveOn = SvcEvent.InstallUninstall_Wait,
                };
            }

            var packageContents = new List <WixEntity>
            {
                new DirFiles(Path.Combine(opts.PackageInDir, MagicStrings.Files.All), path =>
                {
                    var itm = path.ToLower();

                    bool exclude =

                        // configuration will go into mutable location
                        itm.EndsWith(MagicStrings.Ext.DotYml, StringComparison.OrdinalIgnoreCase) ||

                        // we install/remove service ourselves
                        itm.EndsWith(MagicStrings.Ext.DotPs1, StringComparison.OrdinalIgnoreCase) ||

                        // .exe must be excluded for service configuration to work
                        (pc.IsWindowsService && itm.EndsWith(exeName, StringComparison.OrdinalIgnoreCase))
                    ;

                    // this is an "include" filter
                    return(!exclude);
                })
            };

            packageContents.AddRange(
                new DirectoryInfo(opts.PackageInDir)
                .GetDirectories()
                .Select(dir => dir.Name)
                .Except(pc.MutableDirs)
                .Select(dirName =>
                        new Dir(
                            dirName,
                            new Files(Path.Combine(
                                          opts.PackageInDir,
                                          dirName,
                                          MagicStrings.Files.All)))));

            packageContents.Add(pc.IsWindowsService ? service : null);

            // Add a note to the final screen and a checkbox to open the directory of .example.yml file
            var beatConfigExampleFileName = ap.CanonicalTargetName + ".example" + MagicStrings.Ext.DotYml;
            var beatConfigExampleFileId   = beatConfigExampleFileName + "_" + (uint)beatConfigExampleFileName.GetHashCode32();

            project.AddProperty(new Property("WIXUI_EXITDIALOGOPTIONALTEXT",
                                             $"NOTE: Only Administrators can modify configuration files! We put an example configuration file " +
                                             $"in the data directory caled {ap.CanonicalTargetName}.example.yml. Please copy this example file to " +
                                             $"{ap.CanonicalTargetName}.yml and make changes according to your environment. Once {ap.CanonicalTargetName}.yml " +
                                             $"is created, you can configure {ap.CanonicalTargetName} from your favorite shell (in an elevated prompt) " +
                                             $"and then start {serviceDisplayName} Windows service.\r\n"));

            project.AddProperty(new Property("WIXUI_EXITDIALOGOPTIONALCHECKBOX", "1"));
            project.AddProperty(new Property("WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT",
                                             $"Open {ap.CanonicalTargetName} data directory in Windows Explorer"));

            // We'll open the folder for now
            // TODO: select file in explorer window
            project.AddProperty(new Property(
                                    "WixShellExecTarget",
                                    $"[$Component.{beatConfigExampleFileId}]"));

            project.AddWixFragment("Wix/Product",
                                   XElement.Parse(@"
<CustomAction
    Id=""CA_SelectExampleYamlInExplorer""
    BinaryKey = ""WixCA""
    DllEntry = ""WixShellExec""
    Impersonate = ""yes""
/>"),
                                   XElement.Parse(@"
<UI>
    <Publish
        Dialog=""ExitDialog""
        Control=""Finish""
        Event=""DoAction"" 
        Value=""CA_SelectExampleYamlInExplorer"">WIXUI_EXITDIALOGOPTIONALCHECKBOX=1 and NOT Installed
    </Publish>
</UI>"));

            var dataContents = new DirectoryInfo(opts.PackageInDir)
                               .GetFiles(MagicStrings.Files.AllDotYml, SearchOption.TopDirectoryOnly)
                               .Select(fi =>
            {
                var wf = new WixSharp.File(fi.FullName);

                // rename main config file to hide it from MSI engine and keep customizations
                if (string.Compare(
                        fi.Name,
                        ap.CanonicalTargetName + MagicStrings.Ext.DotYml,
                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    wf.Attributes.Add("Name", beatConfigExampleFileName);
                    wf.Id = new Id(beatConfigExampleFileId);
                }

                return(wf);
            })
                               .ToList <WixEntity>();

            dataContents.AddRange(
                pc.MutableDirs
                .Select(dirName =>
            {
                var dirPath = Path.Combine(opts.PackageInDir, dirName);

                return(Directory.Exists(dirPath)
                            ? new Dir(dirName, new Files(Path.Combine(dirPath, MagicStrings.Files.All)))
                            : null);
            })
                .Where(dir => dir != null));

            // Drop CLI shim on disk
            var cliShimScriptPath = Path.Combine(
                opts.PackageOutDir,
                MagicStrings.Files.ProductCliShim(ap.CanonicalTargetName));

            System.IO.File.WriteAllText(cliShimScriptPath, Resources.GenericCliShim);

            var beatsInstallPath =
                $"[ProgramFiles{(ap.Is64Bit ? "64" : string.Empty)}Folder]" +
                Path.Combine(companyName, productSetName);

            project.Dirs = new[]
            {
                // Binaries
                new InstallDir(
                    // Wix# directory parsing needs forward slash
                    beatsInstallPath.Replace("Folder]", "Folder]\\"),
                    new Dir(
                        ap.Version,
                        new Dir(ap.CanonicalTargetName, packageContents.ToArray()),
                        new WixSharp.File(cliShimScriptPath))),

                // Configration and logs
                new Dir("[CommonAppDataFolder]",
                        new Dir(companyName,
                                new Dir(productSetName,
                                        new Dir(ap.CanonicalTargetName, dataContents.ToArray())
                {
                    GenericItems = new []
                    {
                        /*
                         * This will *replace* ACL on the {beatname} directory:
                         *
                         * Directory tree:
                         *  NT AUTHORITY\SYSTEM:(OI)(CI)F
                         *  BUILTIN\Administrators:(OI)(CI)F
                         *  BUILTIN\Users:(CI)R
                         *
                         * Files:
                         *  NT AUTHORITY\SYSTEM:(ID)F
                         *  BUILTIN\Administrators:(ID)F
                         */

                        new MsiLockPermissionEx(
                            "D:PAI(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;CI;0x1200a9;;;BU)",
                            ap.Is64Bit)
                    }
                })))
            };

            // CLI Shim path
            project.Add(new EnvironmentVariable("PATH", Path.Combine(beatsInstallPath, ap.Version))
            {
                Part = EnvVarPart.last
            });

            // We hard-link Wix Toolset to a known location
            Compiler.WixLocation = Path.Combine(opts.BinDir, "WixToolset", "bin");

#if !DEBUG
            if (opts.KeepTempFiles)
#endif
            {
                Compiler.PreserveTempFiles = true;
            }

            if (opts.Verbose)
            {
                Compiler.CandleOptions += " -v";
                Compiler.LightOptions  += " -v";
            }

            project.ResolveWildCards();

            if (opts.WxsOnly)
            {
                project.BuildWxs();
            }
            else if (opts.CmdOnly)
            {
                Compiler.BuildMsiCmd(project, Path.Combine(opts.SrcDir, opts.PackageName) + ".cmd");
            }
            else
            {
                Compiler.BuildMsi(project);
            }
        }
        public override string ToString()
        {
            StringBuilder __sb    = new StringBuilder("TDDIArtifactElementUnion(");
            bool          __first = true;

            if (TerminologyElement != null && __isset.TerminologyElement)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("TerminologyElement: ");
                __sb.Append(TerminologyElement);
            }
            if (AssuranceCasePackage != null && __isset.AssuranceCasePackage)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("AssuranceCasePackage: ");
                __sb.Append(AssuranceCasePackage == null ? "<null>" : AssuranceCasePackage.ToString());
            }
            if (ArtifactPackage != null && __isset.ArtifactPackage)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("ArtifactPackage: ");
                __sb.Append(ArtifactPackage == null ? "<null>" : ArtifactPackage.ToString());
            }
            if (ArtifactGroup != null && __isset.ArtifactGroup)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("ArtifactGroup: ");
                __sb.Append(ArtifactGroup == null ? "<null>" : ArtifactGroup.ToString());
            }
            if (ArtifactAsset != null && __isset.ArtifactAsset)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("ArtifactAsset: ");
                __sb.Append(ArtifactAsset == null ? "<null>" : ArtifactAsset.ToString());
            }
            if (ArgumentationElement != null && __isset.ArgumentationElement)
            {
                if (!__first)
                {
                    __sb.Append(", ");
                }
                __first = false;
                __sb.Append("ArgumentationElement: ");
                __sb.Append(ArgumentationElement == null ? "<null>" : ArgumentationElement.ToString());
            }
            __sb.Append(")");
            return(__sb.ToString());
        }
 public void Write(TProtocol oprot)
 {
     oprot.IncrementRecursionDepth();
     try
     {
         TStruct struc = new TStruct("TDDIArtifactElementUnion");
         oprot.WriteStructBegin(struc);
         TField field = new TField();
         if (TerminologyElement != null && __isset.TerminologyElement)
         {
             field.Name = "TerminologyElement";
             field.Type = TType.Struct;
             field.ID   = 1;
             oprot.WriteFieldBegin(field);
             TerminologyElement.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (AssuranceCasePackage != null && __isset.AssuranceCasePackage)
         {
             field.Name = "AssuranceCasePackage";
             field.Type = TType.Struct;
             field.ID   = 2;
             oprot.WriteFieldBegin(field);
             AssuranceCasePackage.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (ArtifactPackage != null && __isset.ArtifactPackage)
         {
             field.Name = "ArtifactPackage";
             field.Type = TType.Struct;
             field.ID   = 3;
             oprot.WriteFieldBegin(field);
             ArtifactPackage.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (ArtifactGroup != null && __isset.ArtifactGroup)
         {
             field.Name = "ArtifactGroup";
             field.Type = TType.Struct;
             field.ID   = 4;
             oprot.WriteFieldBegin(field);
             ArtifactGroup.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (ArtifactAsset != null && __isset.ArtifactAsset)
         {
             field.Name = "ArtifactAsset";
             field.Type = TType.Struct;
             field.ID   = 5;
             oprot.WriteFieldBegin(field);
             ArtifactAsset.Write(oprot);
             oprot.WriteFieldEnd();
         }
         if (ArgumentationElement != null && __isset.ArgumentationElement)
         {
             field.Name = "ArgumentationElement";
             field.Type = TType.Struct;
             field.ID   = 6;
             oprot.WriteFieldBegin(field);
             ArgumentationElement.Write(oprot);
             oprot.WriteFieldEnd();
         }
         oprot.WriteFieldStop();
         oprot.WriteStructEnd();
     }
     finally
     {
         oprot.DecrementRecursionDepth();
     }
 }