Skip to content

Commit 5d1bca8

Browse files
authored
Merge pull request #2428 from atom-community/temp-directory
2 parents dfc22ff + b298ac0 commit 5d1bca8

File tree

11 files changed

+337
-1076
lines changed

11 files changed

+337
-1076
lines changed

examples/bubble.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Base.Sort
2-
immutable BubbleSortAlg <: Sort.Algorithm end
2+
struct BubbleSortAlg <: Sort.Algorithm end
33
const BubbleSort = BubbleSortAlg()
44

55
function Base.sort!(v::AbstractVector, lo::Int, hi::Int, ::BubbleSortAlg, o::Sort.Ordering)
@@ -15,3 +15,5 @@ while true
1515
end
1616
return v
1717
end
18+
19+
println("Done!")

lib/command-context.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export default class CommandContext {
4848
return commandContext
4949
} // eslint-disable-next-line class-methods-use-this
5050

51-
/** @deprecated use {quoteArguments} function */ quoteArguments(args) {
51+
/** @deprecated use {quoteArguments} function */
52+
/* eslint-disable-next-line class-methods-use-this */
53+
quoteArguments(args) {
5254
return quoteArguments(args)
5355
}
5456

lib/grammar-utils.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,49 @@
22

33
// Require some libs used for creating temporary files
44
import os from "os"
5-
import fs from "fs"
5+
import * as fs from "fs"
66
import path from "path"
7-
import { v1 as uuidv1 } from "uuid"
7+
import * as temp from "temp"
8+
import * as rimraf from "rimraf"
89

910
// Public: GrammarUtils - utilities for determining how to run code
1011
const GrammarUtils = {
1112
tempFilesDir: path.join(os.tmpdir(), "atom_script_tempfiles"),
1213

14+
ensureTempDir() {
15+
if (!fs.existsSync(this.tempFilesDir)) {
16+
fs.mkdirSync(this.tempFilesDir)
17+
}
18+
},
19+
20+
// Public: Create a temporary path
21+
//
22+
// Returns the {String} filepath of the new file
23+
createTempPath(prefix = "", extension = "") {
24+
this.ensureTempDir()
25+
return temp.path({ dir: this.tempFilesDir, prefix, suffix: extension })
26+
},
27+
28+
// Public: Create a temporary directory
29+
//
30+
// Returns the {String} filepath of the new folder
31+
createTempFolder(prefix = "") {
32+
this.ensureTempDir()
33+
return temp.mkdirSync({ dir: this.tempFilesDir, prefix })
34+
},
35+
1336
// Public: Create a temporary file with the provided code
1437
//
1538
// * `code` A {String} containing some code
1639
//
1740
// Returns the {String} filepath of the new file
1841
createTempFileWithCode(code, extension = "") {
1942
try {
20-
if (!fs.existsSync(this.tempFilesDir)) {
21-
fs.mkdirSync(this.tempFilesDir)
22-
}
23-
24-
const tempFilePath = this.tempFilesDir + path.sep + uuidv1() + extension
25-
26-
const file = fs.openSync(tempFilePath, "w")
27-
fs.writeSync(file, code)
28-
fs.closeSync(file)
29-
30-
return tempFilePath
43+
this.ensureTempDir()
44+
const tempFile = temp.openSync({ dir: this.tempFilesDir, suffix: extension })
45+
fs.writeSync(tempFile.fd, code)
46+
fs.closeSync(tempFile.fd)
47+
return tempFile.path
3148
} catch (error) {
3249
throw new Error(`Error while creating temporary file (${error})`)
3350
}
@@ -37,16 +54,9 @@ const GrammarUtils = {
3754
// {GrammarUtils::createTempFileWithCode}
3855
deleteTempFiles() {
3956
try {
40-
if (fs.existsSync(this.tempFilesDir)) {
41-
const files = fs.readdirSync(this.tempFilesDir)
42-
if (files.length) {
43-
files.forEach((file) => fs.unlinkSync(this.tempFilesDir + path.sep + file))
44-
}
45-
return fs.rmdirSync(this.tempFilesDir)
46-
}
47-
return null
57+
rimraf.sync(this.tempFilesDir)
4858
} catch (error) {
49-
throw new Error(`Error while deleting temporary files (${error})`)
59+
console.error(`Error while deleting temporary files (${error})`)
5060
}
5161
},
5262

@@ -63,12 +73,9 @@ const GrammarUtils = {
6373

6474
/** get workingDirectory */
6575
workingDirectory() {
66-
const activePane = atom.workspace.getActivePaneItem()
67-
if (activePane && activePane.buffer && activePane.buffer.file && activePane.buffer.file.getParent) {
68-
const parent = activePane.buffer.file.getParent()
69-
if (parent && parent.getPath) {
70-
return parent.getPath()
71-
}
76+
const textEditor = atom.workspace.getActiveTextEditor()
77+
if (textEditor !== undefined) {
78+
return textEditor.getDirectoryPath()
7279
}
7380
return process.cwd()
7481
},

lib/grammars/c.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ const options = "-Wall -include stdio.h"
1111

1212
// TODO add windows support
1313
function CArgs({ filepath }) {
14+
const tempOutFile = GrammarUtils.createTempPath("c-", ".out")
1415
let cmdArgs = ""
1516
switch (os) {
1617
case "darwin":
17-
cmdArgs = `xcrun clang ${options} -fcolor-diagnostics '${filepath}' -o /tmp/c.out && /tmp/c.out`
18+
cmdArgs = `xcrun clang ${options} -fcolor-diagnostics '${filepath}' -o ${tempOutFile} && ${tempOutFile}`
1819
break
1920
case "linux":
20-
cmdArgs = `cc ${options} '${filepath}' -o /tmp/c.out && /tmp/c.out`
21+
cmdArgs = `cc ${options} '${filepath}' -o ${tempOutFile} && ${tempOutFile}`
2122
break
2223
default: {
2324
atom.notifications.addError(`Not support on ${os}`)
@@ -93,14 +94,14 @@ const Cpp = {
9394
args(context) {
9495
const code = context.getCode()
9596
const tmpFile = GrammarUtils.createTempFileWithCode(code, ".cpp")
97+
const tempOutFile = GrammarUtils.createTempPath("cpp-", ".out")
9698
let cmdArgs = ""
97-
9899
switch (os) {
99100
case "darwin":
100-
cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out`
101+
cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream ${tmpFile} -o ${tempOutFile} && ${tempOutFile}`
101102
break
102103
case "linux":
103-
cmdArgs = `g++ ${options} -std=c++14 -include iostream ${tmpFile} -o /tmp/cpp.out && /tmp/cpp.out`
104+
cmdArgs = `g++ ${options} -std=c++14 -include iostream ${tmpFile} -o ${tempOutFile} && ${tempOutFile}`
104105
break
105106
default: {
106107
atom.notifications.addError(`Not support on ${os}`)
@@ -113,13 +114,14 @@ const Cpp = {
113114
"File Based": {
114115
command,
115116
args({ filepath }) {
117+
const tempOutFile = GrammarUtils.createTempPath("cpp-", ".out")
116118
let cmdArgs = ""
117119
switch (os) {
118120
case "darwin":
119-
cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out`
121+
cmdArgs = `xcrun clang++ -std=c++14 ${options} -fcolor-diagnostics -include iostream '${filepath}' -o ${tempOutFile} && ${tempOutFile}`
120122
break
121123
case "linux":
122-
cmdArgs = `g++ -std=c++14 ${options} -include iostream '${filepath}' -o /tmp/cpp.out && /tmp/cpp.out`
124+
cmdArgs = `g++ -std=c++14 ${options} -include iostream '${filepath}' -o ${tempOutFile} && ${tempOutFile}`
123125
break
124126
case "win32":
125127
if (
@@ -130,7 +132,7 @@ const Cpp = {
130132
filepath = path.posix
131133
.join(...[filepath.split(path.win32.sep)[0].toLowerCase(), ...filepath.split(path.win32.sep).slice(1)])
132134
.replace(":", "")
133-
cmdArgs = `g++ -std=c++14 ${options} -include iostream /mnt/${filepath} -o /tmp/cpp.out && /tmp/cpp.out`
135+
cmdArgs = `g++ -std=c++14 ${options} -include iostream /mnt/${filepath} -o ${tempOutFile} && ${tempOutFile}`
134136
}
135137
break
136138
default: {
@@ -147,9 +149,10 @@ const ObjectiveC = {
147149
"File Based": {
148150
command: "bash",
149151
args({ filepath }) {
152+
const tempOutFile = GrammarUtils.createTempPath("objc-", ".out")
150153
return [
151154
"-c",
152-
`xcrun clang ${options} -fcolor-diagnostics -framework Cocoa '${filepath}' -o /tmp/objc-c.out && /tmp/objc-c.out`,
155+
`xcrun clang ${options} -fcolor-diagnostics -framework Cocoa '${filepath}' -o ${tempOutFile} && ${tempOutFile}`,
153156
]
154157
},
155158
},
@@ -159,9 +162,10 @@ const ObjectiveCpp = {
159162
"File Based": {
160163
command: "bash",
161164
args({ filepath }) {
165+
const tempOutFile = GrammarUtils.createTempPath("objcpp-", ".out")
162166
return [
163167
"-c",
164-
`xcrun clang++ -Wc++11-extensions ${options} -fcolor-diagnostics -include iostream -framework Cocoa '${filepath}' -o /tmp/objc-cpp.out && /tmp/objc-cpp.out`,
168+
`xcrun clang++ -Wc++11-extensions ${options} -fcolor-diagnostics -include iostream -framework Cocoa '${filepath}' -o ${tempOutFile} && ${tempOutFile}`,
165169
]
166170
},
167171
},

lib/grammars/coffeescript.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
"use babel"
22

3-
import path from "path"
3+
import * as path from "path"
44
import GrammarUtils from "../grammar-utils"
55
const { command } = GrammarUtils
66

77
const bin = path.join(__dirname, "../..", "node_modules", ".bin")
88
const coffee = path.join(bin, "coffee")
9-
const babel = path.join(bin, "babel")
9+
const babel = path.join(
10+
__dirname,
11+
"../..",
12+
"node_modules",
13+
".bin",
14+
GrammarUtils.OperatingSystem.isWindows() ? "babel-node.cmd" : "babel-node"
15+
)
1016
const babelConfig = path.join(__dirname, "babel.config.js")
1117

12-
const args = function ({ filepath }) {
13-
const cmd = `'${coffee}' -p '${filepath}'|'${babel}' --filename '${filepath} --config-file ${babelConfig}'| node`
18+
const rimraf = path.join(
19+
__dirname,
20+
"../..",
21+
"node_modules",
22+
".bin",
23+
GrammarUtils.OperatingSystem.isWindows() ? "rimraf.cmd" : "rimraf"
24+
)
25+
26+
function coffeeArgs({ filepath }) {
27+
let filePathOutput = filepath
28+
const extension = filepath.substring(filepath.indexOf(".") + 1)
29+
filePathOutput = filePathOutput.replace(extension, "js")
30+
const cmd = `'${coffee}' --compile '${filepath}' && ${babel} ${filePathOutput} --config-file ${babelConfig}' && ${rimraf} ${filePathOutput}`
1431
return GrammarUtils.formatArgs(cmd)
1532
}
1633

@@ -20,13 +37,14 @@ const CoffeeScript = {
2037
args(context) {
2138
const editor = atom.workspace.getActiveTextEditor()
2239
const scopeName = editor ? editor.getGrammar().scopeName : null
23-
const lit = scopeName !== null && scopeName.includes("lit") ? "lit" : ""
40+
const isLiterate = scopeName !== null && scopeName.includes("lit")
41+
const lit = isLiterate ? "lit" : ""
2442
const code = context.getCode()
2543
const filepath = GrammarUtils.createTempFileWithCode(code, `.${lit}coffee`)
26-
return args({ filepath })
44+
return coffeeArgs({ filepath, isLiterate })
2745
},
2846
},
29-
"File Based": { command, args },
47+
"File Based": { command, args: coffeeArgs },
3048
}
3149

3250
const CoffeeScriptLiterate = CoffeeScript

lib/grammars/fortran.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const FortranFixedForm = {
77
"File Based": {
88
command,
99
args({ filepath }) {
10-
const cmd = `gfortran '${filepath}' -ffixed-form -o /tmp/f.out && /tmp/f.out`
10+
const tempOutFile = GrammarUtils.createTempPath("f-", ".out")
11+
const cmd = `gfortran '${filepath}' -ffixed-form -o ${tempOutFile} && ${tempOutFile}`
1112
return GrammarUtils.formatArgs(cmd)
1213
},
1314
},
@@ -16,7 +17,8 @@ const FortranFreeForm = {
1617
"File Based": {
1718
command,
1819
args({ filepath }) {
19-
const cmd = `gfortran '${filepath}' -ffree-form -o /tmp/f90.out && /tmp/f90.out`
20+
const tempOutFile = GrammarUtils.createTempPath("f90-", ".out")
21+
const cmd = `gfortran '${filepath}' -ffree-form -o ${tempOutFile} && ${tempOutFile}`
2022
return GrammarUtils.formatArgs(cmd)
2123
},
2224
},

lib/grammars/index.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ const OtherGrammars = {
520520
if (windows) {
521521
return [`/c rustc ${filepath} && ${filename.slice(0, Number(-4) + 1 || undefined)}.exe`]
522522
} else {
523-
return ["-c", `rustc '${filepath}' -o /tmp/rs.out && /tmp/rs.out`]
523+
const tempOutFile = GrammarUtils.createTempPath("rs-", ".out")
524+
return ["-c", `rustc '${filepath}' -o ${tempOutFile} && ${tempOutFile}`]
524525
}
525526
},
526527
},
@@ -568,15 +569,21 @@ const OtherGrammars = {
568569
"File Based": {
569570
command: "bash",
570571
args({ filepath }) {
571-
const args = (() => {
572-
switch (arch) {
573-
case "x32":
574-
return `nasm -f elf '${filepath}' -o /tmp/asm.out.o && ld -m elf_i386 /tmp/asm.out.o -o /tmp/asm.out && /tmp/asm.out`
575-
case "x64":
576-
return `nasm -f elf64 '${filepath}' -o /tmp/asm.out.o && ld /tmp/asm.out.o -o /tmp/asm.out && /tmp/asm.out`
572+
const tempOutOFile = GrammarUtils.createTempPath("asm-", ".out.o")
573+
const tempOutFile = GrammarUtils.createTempPath("asm-", ".out")
574+
let cmadArgs = ""
575+
switch (arch) {
576+
case "x32":
577+
cmadArgs = `nasm -f elf '${filepath}' -o ${tempOutOFile} && ld -m elf_i386 ${tempOutOFile} -o ${tempOutFile} && ${tempOutFile}`
578+
break
579+
case "x64":
580+
cmadArgs = `nasm -f elf64 '${filepath}' -o ${tempOutOFile} && ld ${tempOutOFile} -o ${tempOutFile} && ${tempOutFile}`
581+
break
582+
default: {
583+
atom.notifications.addError(`Not supported on ${arch}`)
577584
}
578-
})()
579-
return ["-c", args]
585+
}
586+
return ["-c", cmadArgs]
580587
},
581588
},
582589

@@ -585,15 +592,21 @@ const OtherGrammars = {
585592
args(context) {
586593
const code = context.getCode()
587594
const tmpFile = GrammarUtils.createTempFileWithCode(code, ".asm")
588-
const args = (() => {
589-
switch (arch) {
590-
case "x32":
591-
return `nasm -f elf '${tmpFile}' -o /tmp/asm.out.o && ld -m elf_i386 /tmp/asm.out.o -o /tmp/asm.out && /tmp/asm.out`
592-
case "x64":
593-
return `nasm -f elf64 '${tmpFile}' -o /tmp/asm.out.o && ld /tmp/asm.out.o -o /tmp/asm.out && /tmp/asm.out`
595+
const tempOutOFile = GrammarUtils.createTempPath("asm-", ".out.o")
596+
const tempOutFile = GrammarUtils.createTempPath("asm-", ".out")
597+
let cmdArgs = ""
598+
switch (arch) {
599+
case "x32":
600+
cmdArgs = `nasm -f elf '${tmpFile}' -o ${tempOutOFile} && ld -m elf_i386 ${tempOutOFile} -o ${tempOutFile} && ${tempOutFile}`
601+
break
602+
case "x64":
603+
cmdArgs = `nasm -f elf64 '${tmpFile}' -o ${tempOutOFile} && ld ${tempOutOFile} -o ${tempOutFile} && ${tempOutFile}`
604+
break
605+
default: {
606+
atom.notifications.addError(`Not supported on ${arch}`)
594607
}
595-
})()
596-
return ["-c", args]
608+
}
609+
return ["-c", cmdArgs]
597610
},
598611
},
599612
},

lib/grammars/java.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ const { command } = GrammarUtils
1212

1313
const windows = GrammarUtils.OperatingSystem.isWindows()
1414

15-
const args = function (filepath, jar) {
16-
jar = (jar != null ? jar : path.basename(filepath)).replace(/\.kt$/, ".jar")
17-
const cmd = `kotlinc '${filepath}' -include-runtime -d ${jar} && java -jar ${jar}`
18-
return GrammarUtils.formatArgs(cmd)
19-
}
20-
2115
export const Java = {
2216
"File Based": {
2317
command,
@@ -37,19 +31,25 @@ export const Java = {
3731
},
3832
}
3933

34+
function KotlinArgs(filepath, jar) {
35+
const jarNew = (jar !== null ? jar : path.basename(filepath)).replace(/\.kt$/, ".jar")
36+
const cmd = `kotlinc '${filepath}' -include-runtime -d ${jarNew} && java -jar ${jarNew}`
37+
return GrammarUtils.formatArgs(cmd)
38+
}
39+
4040
export const Kotlin = {
4141
"Selection Based": {
4242
command,
4343
args(context) {
4444
const code = context.getCode()
4545
const tmpFile = GrammarUtils.createTempFileWithCode(code, ".kt")
46-
return args(tmpFile)
46+
return KotlinArgs(tmpFile, null)
4747
},
4848
},
4949
"File Based": {
5050
command,
5151
args({ filepath, filename }) {
52-
return args(filepath, `/tmp/${filename}`)
52+
return KotlinArgs(filepath, path.join(GrammarUtils.createTempFolder("kt-"), filename))
5353
},
5454
},
5555
}

0 commit comments

Comments
 (0)