Пример #1
0
        private void InitializeSubmodulesFromCache(string workingDirectory, string path)
        {
            GitUtils.RunGit(workingDirectory, path, "submodule init");
            var submodules = GitUtils.RunGitAndCapture(workingDirectory, path, "config --local --list");

            foreach (Match match in new Regex(@"submodule\.(?<name>.*)\.url=(?<url>.*)").Matches(submodules))
            {
                var name = match.Groups["name"].Value;
                var url  = match.Groups["url"].Value;

                var submodule = GetSourcePackage(workingDirectory, url);
                GitUtils.RunGit(workingDirectory, path, "config --local submodule." + name + ".url " + submodule);
                GitUtils.RunGit(workingDirectory, path, "submodule update " + name);
                this.InitializeSubmodulesFromCache(workingDirectory, Path.Combine(path ?? "", name));
                GitUtils.RunGit(workingDirectory, path, "config --local submodule." + name + ".url " + url);
            }
        }
Пример #2
0
        public int Execute(object handle)
        {
            var instructions = (List <ParsedInstruction>)handle;

            var protobuild       = Assembly.GetEntryAssembly().Location;
            var workingDirectory = _workingDirectoryProvider.GetPath();

            var    targets              = string.Empty;
            var    buildTarget          = string.Empty;
            var    buildProcessArch     = string.Empty;
            var    buildProperties      = new Dictionary <string, string>();
            string executeConfiguration = null;

            var predicates = new Stack <ParsedInstruction>();

            foreach (var inst in instructions)
            {
                if (inst.Predicate != null)
                {
                    predicates.Push(inst);
                }
                else if (inst.EndPredicate)
                {
                    predicates.Pop();
                }
                else if (predicates.All(x => x.Predicate()))
                {
                    if (inst.Command == "native-execute")
                    {
                        var components = inst.Arguments.Split(new[] { ' ' }, 2);

                        string path;
                        try
                        {
                            path = FindNativeProgram(components[0]);
                        }
                        catch (ApplicationException ex)
                        {
                            Console.Error.WriteLine(ex);
                            return(1);
                        }

                        var args = components.Length == 2 ? components[1] : string.Empty;

                        Console.WriteLine("+ native-execute " + path + " " + args);
                        var process =
                            Process.Start(new ProcessStartInfo(path, args)
                        {
                            WorkingDirectory = workingDirectory,
                            UseShellExecute  = false
                        });
                        if (process == null)
                        {
                            Console.Error.WriteLine("ERROR: Process did not start when running " + path + " " +
                                                    args);
                            return(1);
                        }
                        process.WaitForExit();
                        if (process.ExitCode != 0)
                        {
                            Console.Error.WriteLine(
                                "ERROR: Non-zero exit code " + process.ExitCode);
                            return(process.ExitCode);
                        }
                    }
                    else if (inst.Command == "nuget")
                    {
                        // See if we have a copy of NuGet available for use.
                        var cachedNuget =
                            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                         "NuGet.exe");
                        if (!File.Exists(cachedNuget))
                        {
                            var client = new RetryableWebClient();
                            client.DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", cachedNuget);
                        }

                        string runtime      = null;
                        var    hostPlatform = _hostPlatformDetector.DetectPlatform();
                        if (hostPlatform != "Windows")
                        {
                            try
                            {
                                runtime = FindNativeProgram("mono");
                            }
                            catch (ApplicationException ex)
                            {
                                Console.Error.WriteLine(ex);
                                return(1);
                            }
                        }

                        Process process;

                        if (hostPlatform != "Windows" && runtime != null)
                        {
                            Console.WriteLine("+ " + runtime + " \"" + cachedNuget + "\" " + inst.Arguments);
                            process =
                                Process.Start(new ProcessStartInfo(runtime, "\"" + cachedNuget + "\" " + inst.Arguments)
                            {
                                WorkingDirectory = workingDirectory,
                                UseShellExecute  = false
                            });
                        }
                        else
                        {
                            Console.WriteLine("+ " + cachedNuget + " " + inst.Arguments);
                            process =
                                Process.Start(new ProcessStartInfo(cachedNuget, inst.Arguments)
                            {
                                WorkingDirectory = workingDirectory,
                                UseShellExecute  = false
                            });
                        }

                        if (process == null)
                        {
                            Console.Error.WriteLine(
                                "ERROR: Process did not start when running NuGet with arguments " + inst.Arguments);
                            return(1);
                        }
                        process.WaitForExit();
                        if (process.ExitCode != 0)
                        {
                            Console.Error.WriteLine(
                                "ERROR: Non-zero exit code " + process.ExitCode);
                            return(process.ExitCode);
                        }
                    }
                    else if (inst.Command != null)
                    {
                        var args = string.Empty;
                        switch (inst.Command)
                        {
                        case "execute":
                            if (executeConfiguration != null)
                            {
                                args = "--execute-configuration " + executeConfiguration + " --" + inst.Command +
                                       " " + inst.Arguments;
                            }
                            else
                            {
                                args = "--" + inst.Command + " " + inst.Arguments;
                            }
                            break;

                        case "build":
                            args = "--" + inst.Command + " " + targets + " ";

                            if (buildTarget != string.Empty)
                            {
                                args += "--build-target " + buildTarget + " ";
                            }

                            if (buildProcessArch != string.Empty)
                            {
                                args += "--build-process-arch " + buildProcessArch + " ";
                            }

                            args = buildProperties.Aggregate(args,
                                                             (current, prop) =>
                                                             current + ("--build-property " + prop.Key + " " + prop.Value + " "));
                            args += " " + inst.Arguments;
                            break;

                        case "pack":
                        case "push":
                        case "repush":
                            args = "--" + inst.Command + " " + inst.Arguments;
                            break;

                        default:
                            args = "--" + inst.Command + " " + targets + " " + inst.Arguments;
                            break;
                        }

                        var runSets = new List <string>();
                        if (args.Contains("$TARGET_PLATFORM"))
                        {
                            var targetsSplit = targets.Split(',');
                            if (targetsSplit.Length < 1 || string.IsNullOrWhiteSpace(targetsSplit[0]))
                            {
                                runSets.Add(_hostPlatformDetector.DetectPlatform());
                            }
                            else
                            {
                                runSets.AddRange(targetsSplit);
                            }
                        }
                        else
                        {
                            runSets.Add(_hostPlatformDetector.DetectPlatform());
                        }

                        if (args.Contains("$GIT_COMMIT") || args.Contains("$GIT_BRANCH"))
                        {
                            string commit;
                            string branch;

                            try
                            {
                                Console.WriteLine("+ git rev-parse HEAD");
                                commit = GitUtils.RunGitAndCapture(workingDirectory, "rev-parse HEAD").Trim();

                                try
                                {
                                    Console.WriteLine("+ git show-ref --heads");
                                    var branchesText = GitUtils.RunGitAndCapture(workingDirectory, "show-ref --heads");
                                    var branches     = branchesText.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries);
                                    var branchRaw    = branches.FirstOrDefault(x => x.StartsWith(commit, StringComparison.Ordinal));
                                    branch = string.Empty;
                                    if (branchRaw != null)
                                    {
                                        var branchComponents = branchRaw.Trim().Split(' ');
                                        if (branchComponents.Length >= 2)
                                        {
                                            if (branchComponents[1].StartsWith("refs/heads/", StringComparison.Ordinal))
                                            {
                                                branch = branchComponents[1].Substring("refs/heads/".Length);
                                            }
                                        }
                                    }
                                }
                                catch (InvalidOperationException)
                                {
                                    branch = string.Empty;
                                }
                            }
                            catch (InvalidOperationException)
                            {
                                commit = string.Empty;
                                branch = string.Empty;
                            }

                            args = args.Replace("$GIT_COMMIT", commit);
                            args = args.Replace("$GIT_BRANCH", branch);
                        }

                        string runtime      = null;
                        var    hostPlatform = _hostPlatformDetector.DetectPlatform();
                        if (hostPlatform != "Windows")
                        {
                            try
                            {
                                runtime = FindNativeProgram("mono");
                            }
                            catch (ApplicationException ex)
                            {
                                Console.Error.WriteLine(ex);
                                return(1);
                            }
                        }

                        foreach (var run in runSets)
                        {
                            var runArgs = args
                                          .Replace("$TARGET_PLATFORM", run);

                            Process process;

                            if (hostPlatform != "Windows" && runtime != null)
                            {
                                Console.WriteLine("+ " + runtime + " \"" + protobuild + "\" " + runArgs);
                                process =
                                    Process.Start(new ProcessStartInfo(runtime, "\"" + protobuild + "\" " + runArgs)
                                {
                                    WorkingDirectory = workingDirectory,
                                    UseShellExecute  = false
                                });
                            }
                            else
                            {
                                Console.WriteLine("+ " + protobuild + " " + runArgs);
                                process =
                                    Process.Start(new ProcessStartInfo(protobuild, runArgs)
                                {
                                    WorkingDirectory = workingDirectory,
                                    UseShellExecute  = false
                                });
                            }

                            if (process == null)
                            {
                                Console.Error.WriteLine(
                                    "ERROR: Process did not start when running Protobuild with arguments " + args);
                                return(1);
                            }
                            process.WaitForExit();
                            if (process.ExitCode != 0)
                            {
                                Console.Error.WriteLine(
                                    "ERROR: Non-zero exit code " + process.ExitCode);
                                return(process.ExitCode);
                            }
                        }
                    }
                    else if (inst.Key != null)
                    {
                        Console.WriteLine("+ set " + inst.Key + " -> " + inst.Values.Aggregate((a, b) => a + ", " + b));
                        switch (inst.Key)
                        {
                        case "target-platforms":
                            targets = inst.Values.Aggregate((a, b) => a + "," + b);
                            break;

                        case "build-target":
                            buildTarget = inst.Values.First();
                            break;

                        case "build-process-arch":
                            buildProcessArch = inst.Values.First();
                            break;

                        case "build-property":
                            buildProperties.Add(inst.Values[0],
                                                inst.Values.Length >= 2 ? inst.Values[1] : string.Empty);
                            break;

                        case "execute-configuration":
                            executeConfiguration = inst.Values.First();
                            break;
                        }
                    }
                    else if (inst.Echo != null)
                    {
                        Console.WriteLine(inst.Echo);
                    }
                }
            }

            Console.WriteLine("Automated build script completed successfully.");
            return(0);
        }