Skip to content

Feature/218 #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,34 @@ Simply create a folder somewhere on your system and issue the following command.
dotnet script init
```

This will create `Helloworld.csx` along with the launch configuration needed to debug the script in VS Code.
This will create `main.csx` along with the launch configuration needed to debug the script in VS Code.

```shell
.
├── .vscode
│   └── launch.json
├── helloworld.csx
├── main.csx
└── omnisharp.json
```

We can also initialize a folder using a custom filename.

```shell
dotnet script init custom.csx
```

Instead of `main.csx` which is the default, we now have a file named `custom.csx`.

```shell
.
├── .vscode
│   └── launch.json
├── custom.csx
└── omnisharp.json
```

> Note: Executing `dotnet script init` inside a folder that already contains one or more script files will not create the `main.csx` file.

### Passing arguments to scripts

All arguments after `--` are passed to the script in the following way:
Expand Down
11 changes: 9 additions & 2 deletions src/Dotnet.Script.Core/Scaffolder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Dotnet.Script.Core.Templates;

namespace Dotnet.Script.Core
{
public class Scaffolder
{
public void InitializerFolder()
public void InitializerFolder(string fileName)
{
string currentDirectory = Directory.GetCurrentDirectory();
string vsCodeDirectory = Path.Combine(currentDirectory, ".vscode");
Expand Down Expand Up @@ -35,7 +36,13 @@ public void InitializerFolder()
WriteFile(pathToOmniSharpJson, omniSharpFileTemplate);
}

CreateNewScriptFile("helloworld.csx");
if (Directory.GetFiles(currentDirectory, "*.csx").Any()
&& string.IsNullOrWhiteSpace(fileName))
{
return;
}

CreateNewScriptFile(fileName ?? "main.csx");
}

public void CreateNewScriptFile(string file)
Expand Down
22 changes: 22 additions & 0 deletions src/Dotnet.Script.Tests/DisposableFolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.IO;

namespace Dotnet.Script.Tests
{
public class DisposableFolder : IDisposable
{
public DisposableFolder()
{
var tempFolder = System.IO.Path.GetTempPath();
this.Path = System.IO.Path.Combine(tempFolder, System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetTempFileName()));
Directory.CreateDirectory(Path);
}

public string Path { get; }

public void Dispose()
{
FileUtils.RemoveDirectory(Path);
}
}
}
51 changes: 51 additions & 0 deletions src/Dotnet.Script.Tests/FileUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;

namespace Dotnet.Script.Tests
{
public class FileUtils
{
public static void RemoveDirectory(string path)
{
if (!Directory.Exists(path))
{
return;
}
NormalizeAttributes(path);

foreach (string directory in Directory.GetDirectories(path))
{
RemoveDirectory(directory);
}

try
{
Directory.Delete(path, true);
}
catch (IOException)
{
Directory.Delete(path, true);
}
catch (UnauthorizedAccessException)
{
Directory.Delete(path, true);
}

void NormalizeAttributes(string directoryPath)
{
string[] filePaths = Directory.GetFiles(directoryPath);
string[] subdirectoryPaths = Directory.GetDirectories(directoryPath);

foreach (string filePath in filePaths)
{
File.SetAttributes(filePath, FileAttributes.Normal);
}
foreach (string subdirectoryPath in subdirectoryPaths)
{
NormalizeAttributes(subdirectoryPath);
}
File.SetAttributes(directoryPath, FileAttributes.Normal);
}
}
}
}
50 changes: 32 additions & 18 deletions src/Dotnet.Script.Tests/ScaffoldingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,47 @@ public class ScaffoldingTests
[Fact]
public void ShouldInitializeScriptFolder()
{
var tempFolder = CreateTempFolder();
var result = Execute("init", tempFolder);
Assert.Equal(0, result.exitCode);
Assert.True(File.Exists(Path.Combine(tempFolder, "helloworld.csx")));
Assert.True(File.Exists(Path.Combine(tempFolder, "omnisharp.json")));
Assert.True(File.Exists(Path.Combine(tempFolder, ".vscode", "launch.json")));
Directory.Delete(tempFolder,true);
using (var scriptFolder = new DisposableFolder())
{
var result = Execute("init", scriptFolder.Path);
Assert.Equal(0, result.exitCode);
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "main.csx")));
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "omnisharp.json")));
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, ".vscode", "launch.json")));
}
}

[Fact]
public void ShouldCreateNewScript()
{
var tempFolder = CreateTempFolder();
var result = Execute("new script.csx", tempFolder);
Assert.Equal(0, result.exitCode);
Assert.True(File.Exists(Path.Combine(tempFolder, "script.csx")));
Directory.Delete(tempFolder, true);
using (var scriptFolder = new DisposableFolder())
{
var result = Execute("new script.csx", scriptFolder.Path);
Assert.Equal(0, result.exitCode);
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "script.csx")));
}
}

[Fact]
public void ShouldInitFolderWithCustomFileNAme()
{
using (var scriptFolder = new DisposableFolder())
{
var result = Execute("init custom.csx", scriptFolder.Path);
Assert.Equal(0, result.exitCode);
Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "custom.csx")));
}
}

private static string CreateTempFolder()
[Fact]
public void ShouldNotCreateDefaultFileForFolderWithExistingScriptFiles()
{
var userTempFolder = Path.GetTempPath();
var tempFile = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
var tempFolder = Path.Combine(userTempFolder, tempFile);
Directory.CreateDirectory(tempFolder);
return tempFolder;
using (var scriptFolder = new DisposableFolder())
{
Execute("init custom.csx", scriptFolder.Path);
Execute("init", scriptFolder.Path);
Assert.False(File.Exists(Path.Combine(scriptFolder.Path, "main.csx")));
}
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Dotnet.Script/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ private static int Wain(string[] args)
app.Command("init", c =>
{
c.Description = "Creates a sample script along with the launch.json file needed to launch and debug the script.";
var fileName = c.Argument("filename", "(Optional) The name of the sample script file to be created during initialization. Defaults to 'main.csx'");
c.OnExecute(() =>
{
var scaffolder = new Scaffolder();
scaffolder.InitializerFolder();
scaffolder.InitializerFolder(fileName.Value);
return 0;
});
});
Expand Down