Exemplo n.º 1
0
        private string BuildInternal()
        {
            var nsisMake = DetectNsisMake();

            VerifyMinimumSupportedVersion(nsisMake);

            string regRootKey   = null;
            string regSubKey    = null;
            string regValueName = null;

            if (Prerequisite.RegKeyValue != null)
            {
                var regKeyTokens = Prerequisite.RegKeyValue.Split(':');

                if (regKeyTokens.Length != 3)
                {
                    throw new ArgumentException($"Prerequisite.RegKeyValue: {Prerequisite.RegKeyValue}.\nThis value must comply with the following pattern: <RegistryHive>:<KeyPath>:<ValueName>.");
                }

                regRootKey   = regKeyTokens[0];
                regSubKey    = regKeyTokens[1];
                regValueName = regKeyTokens[2];
            }

            var nsiFile = IO.Path.ChangeExtension(OutputFile, ".nsi");

            if (nsiFile == null)
            {
                throw new InvalidOperationException("NSI script file name can't be null.");
            }

            var builder = new StringBuilder();

            using (var writer = new IO.StringWriter(builder))
            {
                writer.WriteLine("Unicode true");

                AddIncludes(writer);

                AddMacros(writer);

                if (IconFile != null)
                {
                    writer.WriteLine($"Icon \"{IO.Path.GetFullPath(IconFile)}\"");
                }
                writer.WriteLine($"OutFile \"{IO.Path.GetFullPath(OutputFile)}\"");

                writer.WriteLine($"RequestExecutionLevel {ExecutionLevelToString(RequestExecutionLevel)}");
                writer.WriteLine("SilentInstall silent");

                AddVersionInformation(writer);

                writer.WriteLine("Function .onInit");

                writer.WriteLine("InitPluginsDir");

                // Read command line parameters
                writer.WriteLine("${GetParameters} $R0");

                AddSplashScreen(writer);

                AddPrerequisiteFile(writer, regRootKey, regSubKey, regValueName);

                AddPrimaryFile(writer);

                writer.WriteLine("end:");
                writer.WriteLine("FunctionEnd");

                writer.WriteLine("Section");
                writer.WriteLine("SectionEnd");
            }

            NsiSourceGenerated?.Invoke(builder);

            IO.File.WriteAllText(nsiFile, builder.ToString());

            var output = ExecuteNsisMake(nsisMake, $"/INPUTCHARSET UTF8 {nsiFile} {OptionalArguments}");

            if (!string.IsNullOrEmpty(output))
            {
                Console.WriteLine(output);
            }

            // Return null if the OutputFile is failed to generate
            if (!IO.File.Exists(OutputFile))
            {
                return(null);
            }

            if (!Compiler.PreserveTempFiles)
            {
                IO.File.Delete(nsiFile);
            }

            DigitalSignature?.Apply(OutputFile);

            return(OutputFile);
        }