예제 #1
0
        void Handle(string msg, bool doResetGit)
        {
            _logger.Error(msg);
            if (doResetGit)
            {
                _logger.Error("...running git reset to undo any changes...");
                _git.Value.RollbackAllChanges();
            }

            _runtime.Abort();
        }
예제 #2
0
        public (Configuration configuration, string solutionPath) Get(string[] commandLineArguments)
        {
            var solutionFiles = _filesystem.FindSolutionFiles(".", false);

            if (1 != solutionFiles.Length)
            {
                _errors.Handle("Needs to be run from a directory with exactly one *.sln file in it.");
            }
            var solutionPath = Path.GetFullPath(solutionFiles.First());

            switch (ParseCommandLine(commandLineArguments))
            {
            case (_, HelpResult help):
                _console.Info(help.Text);
                _runtime.Abort(help.IsResultOfInvalidInput ? -1 : 0);
                break;

            case (var helpOverview, Configuration configuration):
                if (configuration.OldProjectName.Any(CommonExtensions.IsDirectorySeparator))
                {
                    _errors.Handle(
                        $"Do not specify paths for input/'old' project names, please.{Environment.NewLine}{Environment.NewLine}{helpOverview}");
                }

                configuration.OldProjectName = RemoveProjectFileExtension(configuration.OldProjectName);
                configuration.NewProjectName = RemoveProjectFileExtension(configuration.NewProjectName);

                return(configuration, solutionPath);

            default:
                _errors.Handle(
                    "Something went seriously wrong. Please create an issue at https://github.com/ModernRonin/ProjectRenamer with as much detail as possible.");
                break;
            }

            return(default);
예제 #3
0
        public void Run()
        {
            _git.EnsureIsClean();

            var(wasFound, oldProjectPath, solutionFolderPath) = findProject();
            if (!wasFound)
            {
                _errors.Handle($"{_configuration.OldProjectName} cannot be found in the solution");
            }

            var oldDir  = Path.GetDirectoryName(oldProjectPath);
            var newBase = _configuration.NewProjectName.Any(CommonExtensions.IsDirectorySeparator)
                ? CurrentDirectoryAbsolute
                : Path.GetDirectoryName(oldDir);
            var newDir         = _configuration.NewProjectName.ToAbsolutePath(newBase);
            var newFileName    = Path.GetFileName(_configuration.NewProjectName);
            var newProjectPath = Path.Combine(newDir, $"{newFileName}{Constants.ProjectFileExtension}");
            var isPaketUsed    = _filesystem.DoesDirectoryExist(".paket");
            var gitVersion     = _git.GetVersion();

            if (!_configuration.DontReviewSettings)
            {
                var lines = new[]
                {
                    "Please review the following settings:",
                    $"Project:                   {_configuration.OldProjectName}",
                    $"found at:                  {oldProjectPath}",
                    $"Rename to:                 {newFileName}",
                    $"at:                        {newProjectPath}",
                    $"VS Solution folder:        {solutionFolderPath ?? "none"}",
                    $"exclude:                   {_configuration.ExcludedDirectory}",
                    $"Paket in use:              {isPaketUsed.AsText()}",
                    $"Run paket install:         {(!_configuration.DontRunPaketInstall).AsText()}",
                    $"Run build after rename:    {_configuration.DoRunBuild.AsText()}",
                    $"Create automatic commit:   {(!_configuration.DontCreateCommit).AsText()}",
                    $"Git version:               {gitVersion}",
                    "-----------------------------------------------",
                    "Do you want to continue with the rename operation?"
                };
                if (!_input.AskUser(string.Join(Environment.NewLine, lines)))
                {
                    _runtime.Abort();
                }
            }

            var(dependents, dependencies) = analyzeReferences();
            removeFromSolution();
            removeOldReferences();
            gitMove();
            updatePaketReference();
            addNewReferences();
            addToSolution();
            updatePaket();
            _git.StageAllChanges();
            build();
            commit();

            void addNewReferences()
            {
                dependents.ForEach(p => _dotnet.AddReference(p, newProjectPath));
                dependencies.ForEach(d => _dotnet.AddReference(newProjectPath, d));
            }

            void removeOldReferences()
            {
                dependents.ForEach(p => _dotnet.RemoveReference(p, oldProjectPath));
                dependencies.ForEach(d => _dotnet.RemoveReference(oldProjectPath, d));
            }

            (string[] dependents, string[] dependencies) analyzeReferences()
            {
                _logger.Info(
                    "Analyzing references in your projects - depending on the number of projects this can take a bit...");

                return(
                    allProjects().Where(doesNotEqualOldProjectPath).Where(hasReferenceToOldProject).ToArray(),
                    getReferencedProjects(oldProjectPath).ToArray());

                bool hasReferenceToOldProject(string p) =>
                getReferencedProjects(p).Any(doesEqualOldProjectPath);
            }

            bool doesNotEqualOldProjectPath(string what) => !doesEqualOldProjectPath(what);
            bool doesEqualOldProjectPath(string what) => arePathsEqual(what, oldProjectPath);

            IEnumerable <string> getReferencedProjects(string project)
            {
                var relativeReferences = _dotnet.GetReferencedProjects(project).Select(p => p = Path.Combine(p.Split('\\')));
                var baseDirectory      = Path.GetFullPath(Path.GetDirectoryName(project));

                return(relativeReferences.Select(r => r.ToAbsolutePath(baseDirectory)));
            }

            bool arePathsEqual(string lhs, string rhs) => Path.GetFullPath(lhs) == Path.GetFullPath(rhs);

            void commit()
            {
                if (!_configuration.DontCreateCommit)
                {
                    var wasMove = _configuration.NewProjectName.Any(CommonExtensions.IsDirectorySeparator);
                    var msg     = wasMove
                        ? $"Moved {oldProjectPath.ToRelativePath(CurrentDirectoryAbsolute)} to {newProjectPath.ToRelativePath(CurrentDirectoryAbsolute)}"
                        : $"Renamed {_configuration.OldProjectName} to {_configuration.NewProjectName}";
                    _git.Commit(msg);
                }
            }

            void build()
            {
                if (_configuration.DoRunBuild)
                {
                    _dotnet.BuildSolution(() =>
                    {
                        if (_input.AskUser(
                                "dotnet build returned an error or warning - do you want to rollback all changes?")
                            )
                        {
                            _git.RollbackAllChanges();
                            _runtime.Abort();
                        }
                    });
                }
            }

            void updatePaket()
            {
                if (isPaketUsed && !_configuration.DontRunPaketInstall)
                {
                    _dotnet.PaketInstall();
                }
            }

            void updatePaketReference()
            {
                if (!isPaketUsed)
                {
                    return;
                }
                const string restoreTargets = @"\.paket\Paket.Restore.targets";
                var          nesting        = Path.GetFullPath(newProjectPath).Count(CommonExtensions.IsDirectorySeparator) -
                                              CurrentDirectoryAbsolute.Count(CommonExtensions.IsDirectorySeparator) - 1;
                var paketPath = @"..\".Repeat(nesting)[..^ 1] + restoreTargets;