|
| 1 | +// Author: w0rp <[email protected]> |
| 2 | +// Description: This file provides a D program for implementing |
| 3 | +// the stdin-wrapper on Windows. |
| 4 | + |
| 5 | +import std.algorithm; |
| 6 | +import std.array; |
| 7 | +import std.file; |
| 8 | +import std.process; |
| 9 | +import std.stdio; |
| 10 | +import std.path; |
| 11 | + |
| 12 | +@safe |
| 13 | +auto createTemporaryFilename(string fileExtension) { |
| 14 | + import std.uuid; |
| 15 | + |
| 16 | + string filename; |
| 17 | + |
| 18 | + do { |
| 19 | + const randomPart = randomUUID().toString.replace("-", "_"); |
| 20 | + |
| 21 | + filename = buildPath(tempDir(), "ale_" ~ randomPart ~ fileExtension); |
| 22 | + } while (exists(filename)); |
| 23 | + |
| 24 | + return filename; |
| 25 | +} |
| 26 | + |
| 27 | +@trusted |
| 28 | +void readStdinToFile(ref File tempFile) { |
| 29 | + stdin.byChunk(4096).copy(tempFile.lockingTextWriter()); |
| 30 | +} |
| 31 | + |
| 32 | +// Expand program names like "csslint" to "csslint.cmd" |
| 33 | +// D is not able to perform this kind of expanstion in spawnProcess |
| 34 | +@safe |
| 35 | +string expandedProgramName(string name) { |
| 36 | + auto extArray = environment["PATHEXT"].split(";"); |
| 37 | + |
| 38 | + foreach(pathDir; environment["PATH"].split(";")) { |
| 39 | + foreach(extension; extArray) { |
| 40 | + const candidate = buildPath(pathDir, name ~ extension); |
| 41 | + |
| 42 | + if (exists(candidate)) { |
| 43 | + return candidate; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // We were given a full path for a program name, so use that. |
| 49 | + if (exists(name)) { |
| 50 | + return name; |
| 51 | + } |
| 52 | + |
| 53 | + return ""; |
| 54 | +} |
| 55 | + |
| 56 | +@trusted |
| 57 | +int runLinterProgram(string[] args) { |
| 58 | + const expandedName = expandedProgramName(args[0]); |
| 59 | + |
| 60 | + writeln(expandedName); |
| 61 | + |
| 62 | + if (expandedName) { |
| 63 | + return wait(spawnProcess([expandedName] ~ args[1..$])); |
| 64 | + } |
| 65 | + |
| 66 | + return 1; |
| 67 | +} |
| 68 | + |
| 69 | +@safe |
| 70 | +int main(string[] args) { |
| 71 | + const filename = createTemporaryFilename(args[1]); |
| 72 | + |
| 73 | + auto tempFile = File(filename, "w"); |
| 74 | + |
| 75 | + scope(exit) { |
| 76 | + tempFile.close(); |
| 77 | + remove(filename); |
| 78 | + } |
| 79 | + |
| 80 | + readStdinToFile(tempFile); |
| 81 | + tempFile.close(); |
| 82 | + |
| 83 | + return runLinterProgram(args[2..$] ~ [filename]); |
| 84 | +} |
0 commit comments