Skip to content

Instantly share code, notes, and snippets.

@joshpeterson
Last active December 13, 2015 12:48
Show Gist options
  • Save joshpeterson/9be0cf0eff59c8185571 to your computer and use it in GitHub Desktop.
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
#!/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