Exemple #1
0
        public static async Task <int> ExecuteAsync(
            NewFileOptions fileOptions,
            IConsole console,
            IAppEnvironment appEnvironment,
            InvocationContext context = null)
        {
            var settingsManager = new EnvironmentSettingsManager(appEnvironment);
            EnvironmentSettings environmentSettings = settingsManager.LoadSettings();

            if (environmentSettings.WorkspacePath != null && fileOptions.FilePath != null)
            {
                console.Error.WriteLine("You must either set a workspace via the environment command or supply a filepath.");
                return(ReturnCodes.Error);
            }

            if (fileOptions.FilePath != null)
            {
                environmentSettings.WorkspacePath = fileOptions.FilePath.FullName;
            }

            List <ContentTypeConventionsRoot> conventions = await FindAllConventions(appEnvironment);

            // Select the convention that matches the template name specified
            ContentTypeConventionsRoot contentTypeConventionsRoot = conventions.FirstOrDefault(x => x.Conventions.Any(y => y.Conventions.Any(z => z.Value == fileOptions.TemplateName)));

            // Now find the filepath..
            Convention convention = contentTypeConventionsRoot?.Conventions.SelectMany(x => x.Conventions).FirstOrDefault(x => x.ContentType.StartsWith(ConventionContentTypes.FilePaths, StringComparison.CurrentCultureIgnoreCase));

            if (convention != null)
            {
                IVariableDirectoryPath variablePath = convention.Value.ToVariableDirectoryPath();

                if (variablePath.TryResolve(environmentSettings.ToKvPs(), out IAbsoluteDirectoryPath evaluatedPath) == VariablePathResolvingStatus.Success)
                {
                    IAbsoluteFilePath filepath = evaluatedPath.GetChildFileWithName($"post-{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.md");

                    if (!filepath.ParentDirectoryPath.Exists)
                    {
                        Directory.CreateDirectory(filepath.ParentDirectoryPath.ToString());
                    }

                    string template = Path.Join(contentTypeConventionsRoot.FilePath.ParentDirectoryPath.ParentDirectoryPath.ToString(), contentTypeConventionsRoot?.Conventions.FirstOrDefault()?.TemplatePath);

                    File.Copy(template, filepath.ToString());

                    console.Out.WriteLine($"Created: {filepath}");
                }
            }

            return(ReturnCodes.Ok);
        }
 ///<summary>
 ///Try get a new <see cref="IVariableDirectoryPath"/> object from this string.
 ///</summary>
 ///<returns><i>true</i> if <paramref name="pathString"/> is a valid directory path that contains variables and as a consequence, the returned <paramref name="variableDirectoryPath"/> is not null.</returns>
 ///<remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks>
 ///<param name="pathString">Represents the path.</param>
 ///<param name="variableDirectoryPath">If this method returns <i>true</i>, this is the returned path object.</param>
 public static bool TryGetVariableDirectoryPath(this string pathString, out IVariableDirectoryPath variableDirectoryPath) {
    string failureReasonUnused;
    return pathString.TryGetVariableDirectoryPath(out variableDirectoryPath, out failureReasonUnused);
 }
 ///<summary>
 ///Try get a new <see cref="IVariableDirectoryPath"/> object from this string.
 ///</summary>
 ///<returns><i>true</i> if <paramref name="pathString"/> is a valid directory path that contains variables and as a consequence, the returned <paramref name="variableDirectoryPath"/> is not null.</returns>
 ///<remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks>
 ///<param name="pathString">Represents the path.</param>
 ///<param name="variableDirectoryPath">If this method returns <i>true</i>, this is the returned path object.</param>
 ///<param name="failureReason">If this method returns <i>false</i>, this is the plain english description of the failure.</param>
 public static bool TryGetVariableDirectoryPath(this string pathString, out IVariableDirectoryPath variableDirectoryPath, out string failureReason) {
    variableDirectoryPath = null;
    if (pathString.IsPathStringNullOrEmpty(out failureReason)) { return false; }
    if (!pathString.IsValidVariableDirectoryPath(out failureReason)) { return false; }
    variableDirectoryPath = new VariableDirectoryPath(pathString);
    return true;
 }
		/// <summary>
		///     Try get a new <see cref="IVariableDirectoryPath" /> object from this string.
		/// </summary>
		/// <returns>
		///     <i>true</i> if <paramref name="path" /> is a valid directory path that contains variables and as a consequence, the returned
		///     <paramref name="directoryPath" /> is not null.
		/// </returns>
		/// <remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks>
		/// <param name="path">Represents the path.</param>
		/// <param name="directoryPath">If this method returns <i>true</i>, this is the returned path object.</param>
		public static bool TryGetVariableDirectoryPath(this string path, out IVariableDirectoryPath directoryPath)
		{
			string failureMessage;

			return path.TryGetVariableDirectoryPath(out directoryPath, out failureMessage);
		}
		/// <summary>
		///     Try get a new <see cref="IVariableDirectoryPath" /> object from this string.
		/// </summary>
		/// <returns>
		///     <i>true</i> if <paramref name="path" /> is a valid directory path that contains variables and as a consequence, the returned
		///     <paramref name="directoryPath" /> is not null.
		/// </returns>
		/// <remarks>The path represented by this string doesn't need to exist for this operation to complete properly.</remarks>
		/// <param name="path">Represents the path.</param>
		/// <param name="directoryPath">If this method returns <i>true</i>, this is the returned path object.</param>
		/// <param name="failureMessage">If this method returns <i>false</i>, this is the plain english description of the failure.</param>
		public static bool TryGetVariableDirectoryPath(this string path, out IVariableDirectoryPath directoryPath, out string failureMessage)
		{
			directoryPath = null;

			if (IsNullOrEmpty(() => path, out failureMessage))
			{
				return false;
			}

			if (!path.IsValidVariableDirectoryPath(out failureMessage))
			{
				return false;
			}

			directoryPath = new VariableDirectoryPath(path);

			return true;
		}