Example #1
0
        public string GetShaHashOfCurrentBranch(Context context, TemporaryFilesContext temporaryFilesContext)
        {
            Argument.IsNotNull(() => context);

            string commitSha = null;
            var repositoryDirectory = context.SolutionDirectory;

            if (_repositoryPreparer.IsPreparationRequired(context))
            {
                Log.Info("No local repository is found in '{0}', creating a temporary one", repositoryDirectory);

                repositoryDirectory = _repositoryPreparer.Prepare(context, temporaryFilesContext);
            }

            using (var repository = new Repository(repositoryDirectory))
            {
                if (string.IsNullOrEmpty(context.ShaHash))
                {
                    Log.Info("No sha hash is available on the context, retrieving latest commit of current branch");

                    var lastCommit = repository.Commits.First();
                    commitSha = lastCommit.Sha;
                }
                else
                {
                    Log.Info("Checking if commit with sha hash '{0}' exists on the repository", context.ShaHash);

                    var commit = repository.Commits.FirstOrDefault(c => string.Equals(c.Sha, context.ShaHash, StringComparison.OrdinalIgnoreCase));
                    if (commit != null)
                    {
                        commitSha = commit.Sha;
                    }
                }
            }

            if (commitSha == null)
            {
                throw Log.ErrorAndCreateException<GitLinkException>("Cannot find commit '{0}' in repo.", context.ShaHash);
            }

            return commitSha;
        }
Example #2
0
        public static int Link(Context context)
        {
            int? exitCode = null;

            var stopWatch = new Stopwatch();
            stopWatch.Start();

            context.ValidateContext();

            if (!string.IsNullOrEmpty(context.LogFile))
            {
                var fileLogListener = new FileLogListener(context.LogFile, 25 * 1024);
                fileLogListener.IsDebugEnabled = context.IsDebug;

                fileLogListener.IgnoreCatelLogging = true;
                LogManager.AddListener(fileLogListener);
            }

            using (var temporaryFilesContext = new TemporaryFilesContext())
            {
                Log.Info("Extracting embedded pdbstr.exe");

                var pdbStrFile = temporaryFilesContext.GetFile("pdbstr.exe");
                ResourceHelper.ExtractEmbeddedResource("GitLink.Resources.Files.pdbstr.exe", pdbStrFile);

                try
                {
                    var projects = new List<Project>();
                    string[] solutionFiles;
                    if (string.IsNullOrEmpty(context.SolutionFile))
                    {
                        solutionFiles = Directory.GetFiles(context.SolutionDirectory, "*.sln", SearchOption.AllDirectories);
                    }
                    else
                    {
                        var pathToSolutionFile = Path.Combine(context.SolutionDirectory, context.SolutionFile);
                        if (!File.Exists(pathToSolutionFile))
                        {
                            Log.Error("Could not find solution file: {0}", pathToSolutionFile);
                            return -1;
                        }

                        solutionFiles = new[] { pathToSolutionFile };
                    }

                    foreach (var solutionFile in solutionFiles)
                    {
                        var solutionProjects = ProjectHelper.GetProjects(solutionFile, context.ConfigurationName, context.PlatformName);
                        projects.AddRange(solutionProjects);
                    }

                    var provider = context.Provider;
                    if (provider == null)
                    {
                        throw Log.ErrorAndCreateException<GitLinkException>("Cannot find a matching provider for '{0}'", context.TargetUrl);
                    }

                    Log.Info("Using provider '{0}'", provider.GetType().Name);

                    var shaHash = context.Provider.GetShaHashOfCurrentBranch(context, temporaryFilesContext);

                    Log.Info("Using commit sha '{0}' as version stamp", shaHash);

                    var projectCount = projects.Count();
                    var failedProjects = new List<Project>();
                    Log.Info("Found '{0}' project(s)", projectCount);
                    Log.Info(string.Empty);

                    foreach (var project in projects)
                    {
                        try
                        {
                            var projectName = project.GetProjectName();
                            if (ProjectHelper.ShouldBeIgnored(projectName, context.IncludedProjects, context.IgnoredProjects))
                            {
                                Log.Info("Ignoring '{0}'", project.GetProjectName());
                                Log.Info(string.Empty);
                                continue;
                            }

                            if (context.IsDebug)
                            {
                                project.DumpProperties();
                            }

                            if (!LinkProject(context, project, pdbStrFile, shaHash, context.PdbFilesDirectory))
                            {
                                failedProjects.Add(project);
                            }
                        }
                        catch (Exception)
                        {
                            failedProjects.Add(project);
                        }
                    }

                    Log.Info("All projects are done. {0} of {1} succeeded", projectCount - failedProjects.Count, projectCount);

                    if (failedProjects.Count > 0)
                    {
                        Log.Info(string.Empty);
                        Log.Info("The following projects have failed:");
                        Log.Indent();

                        foreach (var failedProject in failedProjects)
                        {
                            Log.Info("* {0}", context.GetRelativePath(failedProject.GetProjectName()));
                        }

                        Log.Unindent();
                    }

                    exitCode = (failedProjects.Count == 0) ? 0 : -1;
                }
                catch (GitLinkException ex)
                {
                    Log.Error(ex, "An error occurred");
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "An unexpected error occurred");
                }

                stopWatch.Stop();
            }

            Log.Info(string.Empty);
            Log.Info("Completed in '{0}'", stopWatch.Elapsed);

            exitCode = exitCode ?? -1;

            if (context.ErrorsAsWarnings && exitCode != 0)
            {
                Log.Info("One or more errors occurred, but treating it as warning instead");

                exitCode = 0;
            }

            return exitCode.Value;
        }