public override Entity Resolve(Entity owner, string key, bool fallback = false) { if (key == EntityKeys.InternalDeployPathKey) { TargetEntity targetEntity = TargetEntity.Decorate(owner); BuildEntity buildEntity = BuildEntity.Decorate(owner); Entity project = owner.Root; CommandEntity commandOrigin = CommandEntity.Decorate(owner.Origin); VirtualDirectory outputRoot = fileSystem.GetDirectory(commandOrigin.Output, project.Path); VirtualDirectory deployRoot = outputRoot.Directory(targetEntity.FullName.Replace(',', '_'), buildEntity.BuildType); return(owner.Create(key, deployRoot.FullName, deployRoot)); } CommandEntity command = CommandEntity.Decorate(owner); FileEntity projectFileEntity = FileEntity.Decorate(owner.Root); string outputDirectory = command.GetSingleValueArgument(Constants.OutputArgumentName); if (string.IsNullOrEmpty(outputDirectory)) { outputDirectory = projectFileEntity.Directory .Directory(Constants.LibraryFolderName) .FullName; } return(owner.Create(key, outputDirectory)); }
private static string GetBuildType(Entity owner) { CommandEntity commandEntity = CommandEntity.Decorate(owner.Origin); string buildType = commandEntity.GetSingleValueArgument(EntityKeys.BuildTypeKey); if (string.IsNullOrEmpty(buildType)) { buildType = Constants.ReleaseFolderName; } return(buildType); }
public override Entity Resolve(Entity owner, string key, bool fallback = false) { switch (key) { case EntityKeys.ProjectConfigurationsKey: return(GetProjectConfiguration()); case EntityKeys.EngineerVersionKey: return(GetEngineerVersion()); case EntityKeys.SolutionVersionKey: return(GetSolutionVersion()); case EntityKeys.LibraryDescriptionKey: return(GetLibraryDescription()); case EntityKeys.LibraryVersionKey: return(GetLibraryVersion()); default: throw new ContentProviderException(key, owner); } Entity GetProjectConfiguration() { if (!owner.HasPath) { executionContext.WriteVerbose("The executed command lacks the path argument, no project configuration can be loaded."); return(owner.Create(key, new ProjectConfigurations())); } string rootFilePath = fileSystem.GetDirectory(owner.Path, createNew: false) .FullName; string projectDirectory = fileSystem.FileExists(rootFilePath) ? Path.GetDirectoryName(rootFilePath) : rootFilePath; VirtualFile file = fileSystem.DirectoryExists(projectDirectory) && fileSystem.FileExists(Path.Combine(projectDirectory, Constants.ConfigFileName)) ? fileSystem.GetFile(Path.Combine(projectDirectory, Constants.ConfigFileName)) : null; if (file == null) { executionContext.WriteVerbose($"No config file found in {projectDirectory}."); return(owner.Create(key, new ProjectConfigurations())); } try { using (Stream fileStream = file.OpenRead()) using (XmlReader reader = XmlReader.Create(fileStream)) { XmlSerializer serializer = new XmlSerializer(typeof(ProjectConfiguration)); ProjectConfiguration configuration = (ProjectConfiguration)serializer.Deserialize(reader); if (configuration != null) { return(owner.Create(key, new ProjectConfigurations(configuration, file))); } } } catch (DeployArgumentsException) { throw; } catch (Exception e) { executionContext.WriteVerbose($"Error while trying to parse project configuration {file.FullName}." + $"{Environment.NewLine}{e}"); } return(owner.Create(key, new ProjectConfigurations())); } Entity GetEngineerVersion() { CommandEntity command = CommandEntity.Decorate(owner.Origin); ProjectEntity project = ProjectEntity.Decorate(owner); if (command.CommandName.Equals("deploy", StringComparison.OrdinalIgnoreCase) && command.IsCommandArgumentSpecified(Constants.EngineerVersionArgumentKey)) { if (command.IsCommandArgumentSpecified(Constants.SolutionVersionArgumentKey)) { throw new DeployArgumentsException(); } string value = command.GetSingleValueArgument(Constants.EngineerVersionArgumentKey); project.Configuration.EngineerVersion = value; return(owner.Create(key, value)); } return(owner.Create(key, project.Configuration.EngineerVersion)); } Entity GetSolutionVersion() { CommandEntity command = CommandEntity.Decorate(owner.Origin); ProjectEntity project = ProjectEntity.Decorate(owner); if (command.CommandName.Equals("deploy", StringComparison.OrdinalIgnoreCase) && command.IsCommandArgumentSpecified(Constants.SolutionVersionArgumentKey)) { if (command.IsCommandArgumentSpecified(Constants.EngineerVersionArgumentKey)) { throw new DeployArgumentsException(); } string value = command.GetSingleValueArgument(Constants.SolutionVersionArgumentKey); project.Configuration.SolutionVersion = value; return(owner.Create(key, value)); } return(owner.Create(key, project.Configuration.SolutionVersion)); } Entity GetLibraryDescription() { CommandEntity command = CommandEntity.Decorate(owner.Origin); ProjectEntity project = ProjectEntity.Decorate(owner); if (command.CommandName.Equals("deploy", StringComparison.OrdinalIgnoreCase) && command.IsCommandArgumentSpecified(Constants.DescriptionArgumentKey)) { string value = command.GetSingleValueArgument(Constants.DescriptionArgumentKey); project.Configuration.LibraryDescription = value; return(owner.Create(key, value)); } return(owner.Create(key, project.Configuration.LibraryDescription)); } Entity GetLibraryVersion() { CommandEntity command = CommandEntity.Decorate(owner.Origin); ProjectEntity project = ProjectEntity.Decorate(owner); if (command.CommandName.Equals("deploy", StringComparison.OrdinalIgnoreCase) && command.IsCommandArgumentSpecified(Constants.VersionArgumentKey)) { string value = command.GetSingleValueArgument(Constants.VersionArgumentKey); project.Configuration.LibraryVersion = value; return(owner.Create(key, value)); } return(owner.Create(key, project.Configuration.LibraryVersion)); } }
public void DeployFiles(Entity dataModel) { IEnumerable <Entity> deployableEntities = dataModel.Root.Hierarchy(); ProjectEntity project = ProjectEntity.Decorate(dataModel.Root); // get targets to deploy for IEnumerable <Target> targets = null; CommandEntity command = CommandEntity.Decorate(dataModel); if (command.IsCommandArgumentSpecified(Constants.TargetArgumentName)) { IEnumerable <string> rawTargets = command.GetMultiValueArgument(Constants.TargetArgumentName); if (rawTargets.Any()) { targets = targetParser.GetSpecificTargets(rawTargets, false).Select(t => t.Item1); } else { targets = targetParser.Targets(project, false).ValidTargets; } } else { targets = targetParser.Targets(project, false).ValidTargets; } if (!targets.Any()) { throw new NoTargetSpecifiedException(); } foreach (Entity deployableEntity in deployableEntities) { DeployFilesFromTemplate(deployableEntity); } if (command.IsCommandArgumentSpecified(Constants.FilesArgumentName)) { IEnumerable <string> files = command.GetMultiValueArgument(Constants.FilesArgumentName); foreach (string file in files) { DeployFileFromArgument(file); } } void DeployFileFromArgument(string file) { Match match = FilesDecoder.Match(file); if (!match.Success) { throw new FormattableException($"The input {file} could not be parsed. Expected pattern is <fileLocation>|<destination>[|<target>]"); } string from = match.Groups["from"].Value; string to = match.Groups["destination"].Value; string rawTarget = match.Groups["target"].Value; VirtualDirectory baseDirectory = null; string relativePath = null; bool recreateStructure = false; string[] path = fileSystem.GetPath(from); int firstWildCard = path.TakeWhile(p => !p.Contains('*') && !p.Contains('?')).Count(); if (firstWildCard != path.Length) { baseDirectory = fileSystem.GetDirectory(Path.Combine(path.Take(firstWildCard).ToArray()), project.Path, false); relativePath = Path.Combine(path.Skip(firstWildCard).ToArray()); recreateStructure = true; } if (recreateStructure) { IEnumerable <VirtualFile> deployFiles = baseDirectory.Files(relativePath, true).ToArray(); if (!deployFiles.Any()) { throw new DeployFileNotFoundException(@from); } foreach (VirtualFile deployFile in deployFiles) { string structure = Path.GetDirectoryName(deployFile.GetRelativePath(baseDirectory)); string fileDestination = string.IsNullOrEmpty(structure) ? to : Path.Combine(to, structure); DeployFile(deployFile.FullName, fileDestination); } } else if (fileSystem.FileExists(from, project.Path)) { DeployFile(from, to); } else { throw new DeployFileNotFoundException(@from); } void DeployFile(string sourceFile, string destinationDirectory) { if (!string.IsNullOrEmpty(rawTarget)) { DeployFileForRawTarget(rawTarget, sourceFile, destinationDirectory); } else { foreach (Target target in targets) { DeployFileForTarget(target, sourceFile, destinationDirectory); } } } void DeployFileForRawTarget(string target, string sourceFile, string destinationDirectory) { Target parsedTarget = targetParser.ParseTarget(target, null, targets); DeployFileForTarget(parsedTarget, sourceFile, destinationDirectory); } void DeployFileForTarget(Target target, string sourceFile, string destinationDirectory) { VirtualFile fileToCopy = fileSystem.GetFile(sourceFile, project.Path); VirtualFile copiedFile = fileSystem.GetDirectory(destinationDirectory, GetOutputDirectory(target).FullName).File(fileToCopy.Name); using (Stream source = fileToCopy.OpenRead(true)) using (Stream destination = copiedFile.OpenWrite()) { destination.SetLength(0); source.CopyTo(destination); executionContext.WriteVerbose($"Deployed file {fileToCopy.FullName} to {copiedFile.FullName}."); } } } void DeployFilesFromTemplate(Entity deployableEntity) { TemplateDescription template = deployableEntity.Template(); if (template == null) { return; } foreach (templateFile file in template.File) { if (!file.deployPathSpecified) { continue; } VirtualFile deployableFile = GetFile(file, dataModel.Root.Path, false, out string path); DeployFile(file, deployableFile, path); } foreach (templateGeneratedFile generatedFile in template.GeneratedFile ?? Enumerable.Empty <templateGeneratedFile>()) { if (!generatedFile.deployPathSpecified) { continue; } VirtualFile deployableFile = GetFile(generatedFile, dataModel.Root.Path, true, out string path); DeployFile(generatedFile, deployableFile, path); } VirtualFile GetDestination(templateFile file, Target target, string name) { string basePath = GetOutputDirectory(target).FullName; string path = resolver.Resolve(file.deployPath ?? string.Empty, deployableEntity); VirtualFile destination = fileSystem.GetFile(Path.Combine(path, name), basePath); return(destination); } void DeployFile(templateFile file, VirtualFile deployableFile, string filePath) { if (deployableFile == null) { executionContext.WriteVerbose($"Could not find file {filePath} in {dataModel.Root.Path}, the file will not be deployed."); return; } foreach (Target target in targets) { VirtualFile destination = GetDestination(file, target, deployableFile.Name); using (Stream source = deployableFile.OpenRead(true)) using (Stream dest = destination.OpenWrite()) { dest.SetLength(0); source.CopyTo(dest); } executionContext.WriteVerbose($"Deployed file {deployableFile.FullName} to {destination.FullName}."); } } VirtualFile GetFile(templateFile file, string basePath, bool isGeneratedFile, out string realFilePath) { string path = resolver.Resolve(file.path ?? string.Empty, deployableEntity); string name = resolver.Resolve(file.name, deployableEntity); if (isGeneratedFile && file is templateGeneratedFile generatedFile) { realFilePath = Path.Combine(path, name); if (!Path.IsPathRooted(realFilePath)) { realFilePath = Path.Combine(Constants.IntermediateFolderName, generatedFile.generator?.ToLowerInvariant() ?? string.Empty, realFilePath); } } else { realFilePath = Path.Combine(path, name); } VirtualFile destination = fileSystem.FileExists(realFilePath, basePath) ? fileSystem.GetFile(realFilePath, basePath) : null; return(destination); } } VirtualDirectory GetOutputDirectory(Target target) { string buildTypeFolder = command.IsCommandArgumentSpecified(Constants.BuildTypeArgumentName) ? FormatBuildType(command.GetSingleValueArgument(Constants.BuildTypeArgumentName)) : Constants.ReleaseFolderName; string basePath = project.Path; if (!command.IsCommandArgumentSpecified(Constants.OutputArgumentName)) { return(fileSystem.GetDirectory(Path.Combine(basePath, Constants.LibraryFolderName, target.GetFullName().Replace(',', '_'), buildTypeFolder))); } basePath = fileSystem.GetDirectory(command.GetSingleValueArgument(Constants.OutputArgumentName), basePath).FullName; basePath = Path.Combine(basePath, target.GetFullName().Replace(',', '_'), buildTypeFolder); return(fileSystem.GetDirectory(basePath)); string FormatBuildType(string buildType) { if (string.IsNullOrEmpty(buildType)) { return(Constants.ReleaseFolderName); } return(buildType.Substring(0, 1).ToUpperInvariant() + buildType.Substring(1).ToLowerInvariant()); } } }
public override Entity Resolve(Entity owner, string key, bool fallback = false) { switch (key) { case EntityKeys.PathKey: return(GetProjectPath()); case EntityKeys.NameKey: return(GetProjectName()); case EntityKeys.ProjectSettingsKey: return(GetProjectSettings()); case EntityKeys.ProjectVersionKey: return(GetProjectVersion()); case EntityKeys.ProjectIdKey: return(GetProjectId()); default: throw new ContentProviderException(key, owner); } Entity GetProjectId() { CommandEntity command = CommandEntity.Decorate(owner.Origin); if (command.IsCommandArgumentSpecified(Constants.IdArgumentName)) { string id = command.GetSingleValueArgument(Constants.IdArgumentName); if (!Guid.TryParse(id, out Guid guid)) { throw new LibraryIdMalformattedException(id); } return(owner.Create(key, guid, guid.ToString("D", CultureInfo.InvariantCulture))); } ProjectEntity project = ProjectEntity.Decorate(owner); if (!project.Settings.IsPersistent) { executionContext.WriteWarning("The id for the library will change for each generation please use the --id option to set the id."); Guid id = guidFactory.Create(); return(owner.Create(key, id, id.ToString("D", CultureInfo.InvariantCulture))); } string storedId = project.Settings.Value.Id; if (string.IsNullOrEmpty(storedId)) { storedId = guidFactory.Create().ToString("D", CultureInfo.InvariantCulture); project.Settings.SetId(storedId); } Guid result = Guid.Parse(storedId); return(owner.Create(key, result, result.ToString("D", CultureInfo.InvariantCulture))); } Entity GetProjectPath() { ProjectDescription description = owner.Value <ProjectDescription>(); VirtualDirectory path = description?.Root ?? fileSystem.CurrentDirectory; return(owner.Create(key, path.FullName, path)); } Entity GetProjectSettings() { ProjectDescription description = owner.Value <ProjectDescription>(); return(owner.Create(key, new MutableProjectSettings(description.Settings, description.File, executionContext))); } Entity GetProjectName() { ProjectDescription description = owner.Value <ProjectDescription>(); return(owner.Create(key, description?.Root.Name ?? Path.GetFileName(owner.Path))); } Entity GetProjectVersion() { ProjectDescription description = owner.Value <ProjectDescription>(); return(owner.Create(key, Version.Parse(description.Settings.Version))); } }