Log in

No account? Create an account
Previous Entry Share Next Entry
String Replacement Driven by Enum Data

Similar to my previous post "String Replacement Driven by CSV Data" I'd like to give another example which utilizes an enumeration. This approach is limited to replacing valid symbol names, but could be combined with other approaches.

import scriptlike;

enum Data : string {
REPLACEME = "ReplaceValue",
REPLACETHISTOO = "ValueReplaced",

void main() {
    auto file = "MyFile.txt";
    std.file.write(file, file

string replaceWithData(string str) {
    foreach(enumMember; EnumMembers!Data) {
        mixin("str = str.replace((cast(Data)r\""
          ~enumMember~"\").to!string, r\""~enumMember~"\");");
    return str;

Scripting With Dub

Unless I've got this wrong, Dub isn't very well setup for running short scripts. RDMD does a good job of taking a file and building it, but it lacks the ability to grab some nice helper libraries that exist outside the standard library, in my case "scriptlike." This has lead me to, assuming dub to be installed, utilizes a very basic dub.json file to run a script.

    "name": "postchanges",
    "sourcePaths": ["."],
    "targetType": "executable",
    "dependencies": {
        "scriptlike": ">=0.7.0",

Now running 'dub' in the directory will execute the script and include the specified dependencies.