Skip to content

Commit

Permalink
add readonly option
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jun 27, 2019
1 parent d025eb7 commit d545fe4
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 12 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ When scanning for snippets the following are ignored:
* Any of the following directory names: `bin`, `obj`


## Mark resulting files as read only

To mark the resulting `.md` files as read only use `-r` or `--readonly`.

This can be helpful in preventing incorrectly editing the `.md` file instead of the `.source.md` file.

```ps
mdsnippets -r
```


## More Documentation

* [.net API](/docs/api.md)
Expand Down
11 changes: 11 additions & 0 deletions readme.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ When scanning for snippets the following are ignored:
* Any of the following directory names: `bin`, `obj`


## Mark resulting files as read only

To mark the resulting `.md` files as read only use `-r` or `--readonly`.

This can be helpful in preventing incorrectly editing the `.md` file instead of the `.source.md` file.

```ps
mdsnippets -r
```


## More Documentation

* [.net API](/docs/api.md)
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion>
<NoWarn>CS1591</NoWarn>
<NoPackageAnalysis>true</NoPackageAnalysis>
<Version>10.3.0</Version>
<Version>11.0.0</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright $([System.DateTime]::UtcNow.ToString(yyyy)). All rights reserved</Copyright>
<PackageIconUrl>https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/src/icon.png</PackageIconUrl>
Expand Down
7 changes: 6 additions & 1 deletion src/MarkdownSnippets.MsBuild/DocoTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ public class DocoTask :
[Required]
public string ProjectDirectory { get; set; }

public bool ReadOnly { get; set; }

public override bool Execute()
{
var root = GitRepoDirectoryFinder.FindForDirectory(ProjectDirectory);
var processor = new DirectoryMarkdownProcessor(root, log: s => Log.LogMessage(s));
var processor = new DirectoryMarkdownProcessor(
root,
log: s => Log.LogMessage(s),
readOnly: ReadOnly);
try
{
processor.Run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<MarkdownSnippetsAssembly>$(MSBuildThisFileDirectory)..\task\MarkdownSnippets.MsBuild.dll</MarkdownSnippetsAssembly>
<MdTargetDir Condition="'$(SolutionDir)' != '' AND Exists('$(SolutionDir).git')">$(SolutionDir)</MdTargetDir>
<MdTargetDir Condition="'$(SolutionDir)' != '' AND Exists('$(SolutionDir)..\.git')">$(SolutionDir)..\</MdTargetDir>
<MarkdownSnippetsReadOnly Condition="$(MarkdownSnippetsReadOnly) == ''">false</MarkdownSnippetsReadOnly>
</PropertyGroup>

<ItemGroup>
Expand All @@ -21,6 +22,8 @@
<Error
Text="No '.git' directory found. Define a `MdTargetDir`."
Condition="'$(MdTargetDir)' == ''" />
<MarkdownSnippets.DocoTask ProjectDirectory="$(MSBuildProjectDirectory)" />
<MarkdownSnippets.DocoTask
ProjectDirectory="$(MSBuildProjectDirectory)"
ReadOnly="$(MarkdownSnippetsReadOnly)" />
</Target>
</Project>
4 changes: 2 additions & 2 deletions src/MarkdownSnippets.Tool/CommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void RunCommand(Invoke invoke, params string[] args)
var firstArg = args[0];
if (!firstArg.StartsWith('-'))
{
invoke(firstArg, new List<string>());
invoke(firstArg, false, new List<string>());
return;
}
}
Expand All @@ -24,7 +24,7 @@ public static void RunCommand(Invoke invoke, params string[] args)
options =>
{
ApplyDefaults(options);
invoke(options.TargetDirectory, options.Exclude.ToList());
invoke(options.TargetDirectory, options.ReadOnly, options.Exclude.ToList());
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/MarkdownSnippets.Tool/Invoke.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
using System.Collections.Generic;

public delegate void Invoke(string targetDirectory, List<string> exclude);
public delegate void Invoke(string targetDirectory, bool readOnly, List<string> exclude);
5 changes: 4 additions & 1 deletion src/MarkdownSnippets.Tool/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ public class Options
[Option('t', "target-directory", Required = false)]
public string TargetDirectory { get; set; }

[Option('e', "exclude",
[Option('e', "exclude",
Separator = ':',
Required = false,
HelpText = "Directories to be excluded")]
public IList<string> Exclude { get; set; }

[Option('r', "readonly", Required = false, HelpText = "Set output to verbose messages.")]
public bool ReadOnly { get; set; }
}
7 changes: 4 additions & 3 deletions src/MarkdownSnippets.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static void Main(string[] args)
CommandRunner.RunCommand(Inner, args);
}

static void Inner(string targetDirectory, List<string> excludes)
static void Inner(string targetDirectory, bool readOnly, List<string> excludes)
{
Console.WriteLine($"TargetDirectory: {targetDirectory}");
if (!Directory.Exists(targetDirectory))
Expand All @@ -31,9 +31,10 @@ static void Inner(string targetDirectory, List<string> excludes)
try
{
var processor = new DirectoryMarkdownProcessor(
targetDirectory,
targetDirectory,
log: Console.WriteLine,
directoryFilter: path => !excludes.Any(path.Contains));
directoryFilter: path => !excludes.Any(path.Contains),
readOnly: readOnly);
processor.Run();
}
catch (SnippetReadingException exception)
Expand Down
16 changes: 16 additions & 0 deletions src/MarkdownSnippets/FileEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ public static string GetRelativePath(string file, string directory)
var directoryUri = new Uri(directory);
return Uri.UnescapeDataString(directoryUri.MakeRelativeUri(fileUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}

public static void ClearReadOnly(string path)
{
var fileInfo = new FileInfo(path)
{
IsReadOnly = false
};
}

public static void MakeReadOnly(string path)
{
var fileInfo = new FileInfo(path)
{
IsReadOnly = true
};
}
}
11 changes: 10 additions & 1 deletion src/MarkdownSnippets/Processing/DirectoryMarkdownProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class DirectoryMarkdownProcessor
{
bool writeHeader;
DirectoryFilter directoryFilter;
bool readOnly;
Action<string> log;
string targetDirectory;
List<string> sourceMdFiles = new List<string>();
Expand All @@ -25,10 +26,12 @@ public class DirectoryMarkdownProcessor
Action<string> log = null,
AppendSnippetGroupToMarkdown appendSnippetGroup = null,
bool writeHeader = true,
DirectoryFilter directoryFilter = null)
DirectoryFilter directoryFilter = null,
bool readOnly = true)
{
this.writeHeader = writeHeader;
this.directoryFilter = directoryFilter;
this.readOnly = readOnly;
if (appendSnippetGroup == null)
{
this.appendSnippetGroup = new SnippetMarkdownHandling(targetDirectory).AppendGroup;
Expand Down Expand Up @@ -135,6 +138,7 @@ void ProcessFile(string sourceFile, MarkdownProcessor markdownProcessor)
{
log($"Processing {sourceFile}");
var target = GetTargetFile(sourceFile, targetDirectory);
FileEx.ClearReadOnly(target);
using (var reader = File.OpenText(sourceFile))
using (var writer = File.CreateText(target))
{
Expand All @@ -150,6 +154,11 @@ void ProcessFile(string sourceFile, MarkdownProcessor markdownProcessor)
throw new MissingSnippetsException(missing);
}
}

if (readOnly)
{
FileEx.MakeReadOnly(target);
}
}

static string GetTargetFile(string sourceFile, string rootDirectory)
Expand Down
29 changes: 28 additions & 1 deletion src/Tests/Console/CommandRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ public class CommandRunnerTests :
{
string targetDirectory;
List<string> exclude;
bool readOnly;

[Fact]
public void Empty()
{
CommandRunner.RunCommand(Capture);
Assert.Equal(Environment.CurrentDirectory, targetDirectory);
Assert.Empty(exclude);
Assert.False(readOnly);
}

[Fact]
Expand All @@ -25,34 +27,57 @@ public void SingleUnNamedArg()
CommandRunner.RunCommand(Capture, "dir");
Assert.Equal("dir", targetDirectory);
Assert.Empty(exclude);
Assert.False(readOnly);
}

[Fact]
public void ReadOnlyShort()
{
CommandRunner.RunCommand(Capture, "-r");
Assert.Equal(Environment.CurrentDirectory, targetDirectory);
Assert.Empty(exclude);
Assert.True(readOnly);
}

[Fact]
public void ReadOnlyLong()
{
CommandRunner.RunCommand(Capture, "--readonly");
Assert.Equal(Environment.CurrentDirectory, targetDirectory);
Assert.Empty(exclude);
Assert.True(readOnly);
}

[Fact]
public void TargetDirectoryShort()
{
CommandRunner.RunCommand(Capture, "-t", "dir");
Assert.Equal(Path.GetFullPath("dir"), targetDirectory);
Assert.False(readOnly);
}

[Fact]
public void TargetDirectoryLong()
{
CommandRunner.RunCommand(Capture, "--target-directory", "dir");
Assert.Equal(Path.GetFullPath("dir"), targetDirectory);
Assert.False(readOnly);
}

[Fact]
public void ExcludeShort()
{
CommandRunner.RunCommand(Capture, "-e", "dir");
Assert.Equal("dir", exclude.Single());
Assert.False(readOnly);
}

[Fact]
public void ExcludeMultiple()
{
CommandRunner.RunCommand(Capture, "-e", "dir1:dir2");
Assert.Equal(new List<string> {"dir1", "dir2"}, exclude);
Assert.False(readOnly);
}

[Fact]
Expand All @@ -72,12 +97,14 @@ public void ExcludeLong()
{
CommandRunner.RunCommand(Capture, "--exclude", "dir");
Assert.Equal("dir", exclude.Single());
Assert.False(readOnly);
}

void Capture(string targetDirectory, List<string> exclude)
void Capture(string targetDirectory, bool readOnly, List<string> exclude)
{
this.targetDirectory = targetDirectory;
this.exclude = exclude;
this.readOnly = readOnly;
}

public CommandRunnerTests(ITestOutputHelper output) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ public void Run()
processor.Run();
}

[Fact]
public void ReadOnly()
{
var root = Path.GetFullPath("DirectoryMarkdownProcessor/Readonly");
var processor = new DirectoryMarkdownProcessor(root, scanForSnippets: false, writeHeader: false);
processor.IncludeSnippets(
SnippetBuild("snippet1"),
SnippetBuild("snippet2")
);
processor.Run();

Assert.True(new FileInfo(Path.Combine(root, "one.md")).IsReadOnly);
}

[Fact]
public void Convention()
{
Expand Down
7 changes: 7 additions & 0 deletions src/Tests/DirectoryMarkdownProcessor/ReadOnly/one.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--
This file was generate by MarkdownSnippets.
Source File: /src/Tests/GitHubMarkdownProcessor/Convention/one.source.md
To change this file edit the source file and then re-run the generation using either the dotnet global tool (https://github.com/SimonCropp/MarkdownSnippets#markdownsnippetstool) or using the api (https://github.com/SimonCropp/MarkdownSnippets#running-as-a-unit-test).
-->
<!-- snippet: snippet1 -->
** Could not find snippet 'snippet1' **
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snippet: snippet1

0 comments on commit d545fe4

Please sign in to comment.