summaryrefslogtreecommitdiff
path: root/stdin_wrapper.d
blob: 8714bc2aee3be0614e263602f8aeebe58f77739c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Author: w0rp <devw0rp@gmail.com>
// Description: This file provides a D program for implementing
//              the stdin-wrapper on Windows.

import std.algorithm;
import std.array;
import std.file;
import std.process;
import std.stdio;
import std.path;

@safe
auto createTemporaryFilename(string fileExtension) {
    import std.uuid;

    string filename;

    do {
        const randomPart = randomUUID().toString.replace("-", "_");

        filename = buildPath(tempDir(), "ale_" ~ randomPart ~ fileExtension);
    } while (exists(filename));

    return filename;
}

@trusted
void readStdinToFile(ref File tempFile) {
    stdin.byChunk(4096).copy(tempFile.lockingTextWriter());
}

// Expand program names like "csslint" to "csslint.cmd"
// D is not able to perform this kind of expanstion in spawnProcess
@safe
string expandedProgramName(string name) {
    auto extArray = environment["PATHEXT"].split(";");

    foreach(pathDir; environment["PATH"].split(";")) {
        foreach(extension; extArray) {
            const candidate = buildPath(pathDir, name ~ extension);

            if (exists(candidate)) {
                return candidate;
            }
        }
    }

    // We were given a full path for a program name, so use that.
    if (exists(name)) {
        return name;
    }

    return "";
}

@trusted
int runLinterProgram(string[] args) {
    const expandedName = expandedProgramName(args[0]);

    writeln(expandedName);

    if (expandedName) {
        return wait(spawnProcess([expandedName] ~ args[1..$]));
    }

    return 1;
}

@safe
int main(string[] args) {
    const filename = createTemporaryFilename(args[1]);

    auto tempFile = File(filename, "w");

    scope(exit) {
        tempFile.close();
        remove(filename);
    }

    readStdinToFile(tempFile);
    tempFile.close();

    return runLinterProgram(args[2..$] ~ [filename]);
}