Example #1
0
        public bool Build(string outputPath)
        {
            if (_project.Repository == null)
            {
                // Nothing to do
                return(true);
            }

            _reports.Information.WriteLine("Embedding repository information...");

            string repositoryType;

            if (!_project.Repository.TryGetValue(Constants.RepoTypeKey, out repositoryType) ||
                string.IsNullOrEmpty(repositoryType))
            {
                WriteError("The project file contains a repository property but the repository type is missing.");
                return(false);
            }

            var sourceControlProvider = SourceControlProviderFactory.ResolveProvider(repositoryType, _reports);

            if (sourceControlProvider == null)
            {
                WriteError($"'{repositoryType}' repository type is not supported.");
                return(false);
            }

            if (!sourceControlProvider.IsInstalled)
            {
                WriteWarning($"Could not find the client for '{repositoryType}' repositories. The repository information will not be included in the package.");
                return(true);
            }

            var projectFolder = _project.ProjectDirectory;

            if (!sourceControlProvider.IsRepository(projectFolder))
            {
                WriteWarning("The project is not under a repository. The repository information will not be included in the package.");
                return(true);
            }

            sourceControlProvider.AddMissingSnapshotInformation(projectFolder, _project.Repository);

            string snapshotFileContent = JsonConvert.SerializeObject(_project.Repository, Formatting.Indented);
            var    snapshotFile        = Path.Combine(
                outputPath,
                Constants.SnapshotInfoFileName);

            File.WriteAllText(snapshotFile, snapshotFileContent);

            _reports.Verbose.WriteLine($"Repository information wrote to '{snapshotFile}'.");

            _packageBuilder.Files.Add(new PhysicalPackageFile()
            {
                SourcePath = snapshotFile,
                TargetPath = Constants.SnapshotInfoFileName
            });

            return(true);
        }
Example #2
0
        public bool Execute()
        {
            var projectFile = ResolveProjectFile();

            if (string.IsNullOrEmpty(projectFile))
            {
                return(false);
            }

            _solutionRoot = ProjectResolver.ResolveRootDirectory(projectFile);

            var globalFile = GlobalSettings.GetGlobalFilePath(_solutionRoot);

            if (!File.Exists(globalFile))
            {
                _reports.WriteError($"The '{GlobalSettings.GlobalFileName}' is missing from '{_solutionRoot}'.");
                return(false);
            }

            var packagesFolder = ResolvePackagesFolder();

            if (string.IsNullOrEmpty(packagesFolder))
            {
                return(false);
            }

            var packageVersion = ResolvePackageVersion(projectFile);

            if (packageVersion == null)
            {
                return(false);
            }

            var packageResolver = new DefaultPackagePathResolver(packagesFolder);
            var packageFolder   = packageResolver.GetPackageDirectory(_packageId, packageVersion);

            packageFolder = Path.Combine(packagesFolder, packageFolder);

            var snapshotInfo = ReadRepositoryInfo(packageFolder);

            if (snapshotInfo == null)
            {
                return(false);
            }

            string repoType;

            if (!snapshotInfo.TryGetValue(Constants.RepoTypeKey, out repoType))
            {
                _reports.WriteError("Repository type information is missing from the repository information file.");
                return(false);
            }

            var provider = SourceControlProviderFactory.ResolveProvider(repoType, _reports);

            if (provider == null)
            {
                _reports.WriteError($"Unknown repository type '{repoType}'");
                return(false);
            }

            if (!provider.IsInstalled)
            {
                _reports.WriteError($"The '{repoType}' client application is not installed.");
                return(false);
            }

            var sourcesFolder = ResolveSourcesFolder();

            var sourceFolderName          = provider.CreateShortFolderName(snapshotInfo);
            var sourceDestinationFullPath = Path.Combine(sourcesFolder, sourceFolderName);

            if (!Directory.Exists(sourceDestinationFullPath))
            {
                _reports.WriteInformation($"Downloading sources in '{sourceDestinationFullPath}'...");
                if (!provider.GetSources(sourceDestinationFullPath, snapshotInfo))
                {
                    return(false);
                }
            }
            else
            {
                _reports.WriteInformation($"Sources already found in '{sourceDestinationFullPath}'");
            }

            var srcFolder = provider.GetSourceFolderPath(snapshotInfo);

            srcFolder = Path.Combine(sourceDestinationFullPath, srcFolder);
            if (!Directory.Exists(srcFolder))
            {
                _reports.WriteError($"The source code folder '{srcFolder}' is missing.");
                return(false);
            }

            _reports.Verbose.WriteLine($"Updating {GlobalSettings.GlobalFileName}...");
            ModifyJson(globalFile, jObj =>
            {
                var projects = jObj["projects"] as JArray;
                if (projects == null)
                {
                    projects = new JArray();
                    projects.Add(srcFolder);
                    jObj.Add("projects", projects);
                }
                else
                {
                    if (!projects.Any(t => t.ToString().Equals(srcFolder)))
                    {
                        projects.Add(srcFolder);
                    }
                }
            });

            return(true);
        }