Skip to content

Commit 6d85b98

Browse files
committed
Added support for custom filename during init
1 parent 17b13f5 commit 6d85b98

File tree

5 files changed

+116
-21
lines changed

5 files changed

+116
-21
lines changed

src/Dotnet.Script.Core/Scaffolder.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Reflection;
45
using Dotnet.Script.Core.Templates;
56

67
namespace Dotnet.Script.Core
78
{
89
public class Scaffolder
910
{
10-
public void InitializerFolder()
11+
public void InitializerFolder(string fileName)
1112
{
1213
string currentDirectory = Directory.GetCurrentDirectory();
1314
string vsCodeDirectory = Path.Combine(currentDirectory, ".vscode");
@@ -35,7 +36,13 @@ public void InitializerFolder()
3536
WriteFile(pathToOmniSharpJson, omniSharpFileTemplate);
3637
}
3738

38-
CreateNewScriptFile("helloworld.csx");
39+
if (Directory.GetFiles(currentDirectory, "*.csx", SearchOption.AllDirectories).Any()
40+
&& string.IsNullOrWhiteSpace(fileName))
41+
{
42+
return;
43+
}
44+
45+
CreateNewScriptFile(fileName ?? "main.csx");
3946
}
4047

4148
public void CreateNewScriptFile(string file)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Dotnet.Script.Tests
5+
{
6+
public class DisposableFolder : IDisposable
7+
{
8+
public DisposableFolder()
9+
{
10+
var tempFolder = System.IO.Path.GetTempPath();
11+
this.Path = System.IO.Path.Combine(tempFolder, System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetTempFileName()));
12+
Directory.CreateDirectory(Path);
13+
}
14+
15+
public string Path { get; }
16+
17+
public void Dispose()
18+
{
19+
FileUtils.RemoveDirectory(Path);
20+
}
21+
}
22+
}

src/Dotnet.Script.Tests/FileUtils.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Dotnet.Script.Tests
5+
{
6+
public class FileUtils
7+
{
8+
public static void RemoveDirectory(string path)
9+
{
10+
if (!Directory.Exists(path))
11+
{
12+
return;
13+
}
14+
NormalizeAttributes(path);
15+
16+
foreach (string directory in Directory.GetDirectories(path))
17+
{
18+
RemoveDirectory(directory);
19+
}
20+
21+
try
22+
{
23+
Directory.Delete(path, true);
24+
}
25+
catch (IOException)
26+
{
27+
Directory.Delete(path, true);
28+
}
29+
catch (UnauthorizedAccessException)
30+
{
31+
Directory.Delete(path, true);
32+
}
33+
34+
void NormalizeAttributes(string directoryPath)
35+
{
36+
string[] filePaths = Directory.GetFiles(directoryPath);
37+
string[] subdirectoryPaths = Directory.GetDirectories(directoryPath);
38+
39+
foreach (string filePath in filePaths)
40+
{
41+
File.SetAttributes(filePath, FileAttributes.Normal);
42+
}
43+
foreach (string subdirectoryPath in subdirectoryPaths)
44+
{
45+
NormalizeAttributes(subdirectoryPath);
46+
}
47+
File.SetAttributes(directoryPath, FileAttributes.Normal);
48+
}
49+
}
50+
}
51+
}

src/Dotnet.Script.Tests/ScaffoldingTests.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,47 @@ public class ScaffoldingTests
88
[Fact]
99
public void ShouldInitializeScriptFolder()
1010
{
11-
var tempFolder = CreateTempFolder();
12-
var result = Execute("init", tempFolder);
13-
Assert.Equal(0, result.exitCode);
14-
Assert.True(File.Exists(Path.Combine(tempFolder, "helloworld.csx")));
15-
Assert.True(File.Exists(Path.Combine(tempFolder, "omnisharp.json")));
16-
Assert.True(File.Exists(Path.Combine(tempFolder, ".vscode", "launch.json")));
17-
Directory.Delete(tempFolder,true);
11+
using (var scriptFolder = new DisposableFolder())
12+
{
13+
var result = Execute("init", scriptFolder.Path);
14+
Assert.Equal(0, result.exitCode);
15+
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "main.csx")));
16+
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "omnisharp.json")));
17+
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, ".vscode", "launch.json")));
18+
}
1819
}
1920

2021
[Fact]
2122
public void ShouldCreateNewScript()
2223
{
23-
var tempFolder = CreateTempFolder();
24-
var result = Execute("new script.csx", tempFolder);
25-
Assert.Equal(0, result.exitCode);
26-
Assert.True(File.Exists(Path.Combine(tempFolder, "script.csx")));
27-
Directory.Delete(tempFolder, true);
24+
using (var scriptFolder = new DisposableFolder())
25+
{
26+
var result = Execute("new script.csx", scriptFolder.Path);
27+
Assert.Equal(0, result.exitCode);
28+
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "script.csx")));
29+
}
2830
}
2931

32+
[Fact]
33+
public void ShouldInitFolderWithCustomFileNAme()
34+
{
35+
using (var scriptFolder = new DisposableFolder())
36+
{
37+
var result = Execute("init custom.csx", scriptFolder.Path);
38+
Assert.Equal(0, result.exitCode);
39+
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "custom.csx")));
40+
}
41+
}
3042

31-
private static string CreateTempFolder()
43+
[Fact]
44+
public void ShouldNotCreateDefaultFileForFolderWithExistingScriptFiles()
3245
{
33-
var userTempFolder = Path.GetTempPath();
34-
var tempFile = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
35-
var tempFolder = Path.Combine(userTempFolder, tempFile);
36-
Directory.CreateDirectory(tempFolder);
37-
return tempFolder;
46+
using (var scriptFolder = new DisposableFolder())
47+
{
48+
Execute("init custom.csx", scriptFolder.Path);
49+
Execute("init", scriptFolder.Path);
50+
Assert.False(File.Exists(Path.Combine(scriptFolder.Path, "main.csx")));
51+
}
3852
}
3953

4054
/// <summary>

src/Dotnet.Script/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ private static int Wain(string[] args)
8888
app.Command("init", c =>
8989
{
9090
c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
91+
var fileName = c.Argument("filename", "The name of the sample script file to be created during initialization.");
9192
c.OnExecute(() =>
9293
{
9394
var scaffolder = new Scaffolder();
94-
scaffolder.InitializerFolder();
95+
scaffolder.InitializerFolder(fileName.Value);
9596
return 0;
9697
});
9798
});

0 commit comments

Comments
 (0)