Last active
December 13, 2015 12:48
-
-
Save joshpeterson/9be0cf0eff59c8185571 to your computer and use it in GitHub Desktop.
A Ruby script to generate a new C# class and test based on a namespace and a class name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
templateCs = %{namespace <namespace> | |
{ | |
public class <class> | |
{ | |
} | |
} | |
} | |
templateTest = %{using NUnit.Framework; | |
namespace <namespace>.Tests | |
{ | |
[TestFixture] | |
public class <class>Tests | |
{ | |
[Test] | |
public void TheTruth() | |
{ | |
Assert.That(true, Is.True); | |
} | |
} | |
} | |
} | |
def replaceNamespace(namespace, template) | |
template.gsub("<namespace>", namespace) | |
end | |
def replaceClass(klass, template) | |
template.gsub("<class>", klass) | |
end | |
if (ARGV.length != 2) | |
puts "Usage: newcs.rb namespace class\n" | |
exit | |
end | |
namespace = ARGV[0] | |
klass = ARGV[1] | |
codeDirectory = namespace; | |
if (!Dir.exists?(codeDirectory)) | |
puts "Error: The directory '#{codeDirectory}' does not exist.\n" | |
exit | |
end | |
testDirectory = "#{namespace}.Tests" | |
if (!Dir.exists?(testDirectory)) | |
puts "Error: The directory '#{testDirectory}' does not exist.\n" | |
exit | |
end | |
codeFile = "#{codeDirectory}/#{klass}.cs" | |
if (File.exists?(codeFile)) | |
puts "Error: The file '#{codeFile}' already exists.\n" | |
exit | |
end | |
testFile = "#{testDirectory}/#{klass}Tests.cs" | |
if (File.exists?(testFile)) | |
puts "Error: The file '#{testFile}' already exists.\n" | |
exit | |
end | |
File.open(codeFile, 'w') { |file| | |
file.write(replaceClass(klass, replaceNamespace(namespace, templateCs))) | |
} | |
File.open(testFile, 'w') { |file| | |
file.write(replaceClass(klass, replaceNamespace(namespace, templateTest))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment