Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New features : precise multifile compilation and output directory #208

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extends:
extends:
- eslint:all
parserOptions:
ecmaVersion: 6
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ git commit -m "added a feature"
git push
# go to https://github.com/<your username>/atom-gpp-compiler.git and click `Create Pull Request`
```

127 changes: 119 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* global atom */
"use strict";


const child_process = require("child_process");
const fs = require("fs");
const path = require("path");
const os = require("os");
const readLine = require("readline");
// const {CompositeDisposable} = require("atom");
const CompositeDisposable = require("atom").CompositeDisposable;

Expand Down Expand Up @@ -44,6 +46,12 @@ module.exports = {
title: "Add `compiling_error.txt`",
type: "boolean"
},
useMultifileCompiling: {
default: false,
description: "Read the <strong>compilerInfos</Strong> file in the main folder and add the listed sources to the compiler command.",
title: "Use multifile compiling options",
type: "boolean"
},
debug: {
default: false,
description: "Logs function calls in console.",
Expand Down Expand Up @@ -79,6 +87,12 @@ module.exports = {
title: "C Compiler",
type: "string"
},
compileDirectory: {
default: "",
description: "Directory of the executable output",
title: "Output directory",
type: "string"
},
cppCompiler: {
default: "g++",
title: "C++ Compiler",
Expand Down Expand Up @@ -212,12 +226,82 @@ function compileFile(fileType, gdb) {
if (file) {
const filePath = file.path;
const info = path.parse(filePath);
let optionsPath = "";

// if user wants to choose sources to compile
if (atom.config.get("gpp-compiler.useMultifileCompiling")) {
// get config file path
optionsPath = info.dir;
optionsPath += "/compilerInfos";

// init line reader
const lineReader = readLine.createInterface({
input: fs.createReadStream(optionsPath)
});

// source files in this array
const res = [];
let i = 0;

// check if compilerInfos exists
if (!fs.existsSync(optionsPath)) {
atom.notifications.addError("<strong>Use Multifile Compiling option</strong> is on<br/> create the file <strong>compilerInfos</strong> next to your main before compiling");
}

// get all files to add in compile command
lineReader.on("line", (line) => {
// we don't take the first line, for now it is just "file :", could add other options later if necessary
if (i > 0) {
let temp = line;

// get rid of spaces and tabulations
temp = temp.replace(" ", "");
temp = temp.replace(" ", "");

// add to array
res.push(temp);
}
i++;
});

// when all lines are red
lineReader.on("close", () => {
// find the compile path
let compilePath = "";

if (atom.config.get("gpp-compiler.compileDirectory") === "") {
compilePath = getCompiledPath(info.dir, info.name);
} else {
compilePath = getCompiledPath(atom.config.get("gpp-compiler.compileDirectory"), info.name);
}

// generate args with the new path
const args = getArgs([filePath], compilePath, fileType, gdb ? ["-g"] : null);

compile(getCommand(fileType), info, getArgs([
filePath
], getCompiledPath(info.dir, info.name), fileType, gdb ? [
"-g"
] : null), gdb);
// add the additionnal sources to the args
for (const r of res) {
args.push(r);
}

// compile
compile(getCommand(fileType), info, args, gdb);
});
} else {
// find the compile path
let compilePath = "";

if (atom.config.get("gpp-compiler.compileDirectory") === "") {
compilePath = getCompiledPath(info.dir, info.name);
} else {
compilePath = getCompiledPath(atom.config.get("gpp-compiler.compileDirectory"), info.name);
}

// set the args with the new path
const args = getArgs([filePath], compilePath, fileType, gdb ? ["-g"] : null);

// compile
compile(getCommand(fileType), info, args, gdb);
}
} else {
atom.
notifications.
Expand Down Expand Up @@ -319,7 +403,15 @@ function compile(command, info, args, gdb) {
const terminal = atom.
config.
get("gpp-compiler.linuxTerminal");
const file = getCompiledPath(info.dir, info.name);

// find the executable path
let file = "";

if (atom.config.get("gpp-compiler.compileDirectory") === "") {
file = getCompiledPath(info.dir, info.name);
} else {
file = getCompiledPath(atom.config.get("gpp-compiler.compileDirectory"), info.name);
}

let terminalCommand = null;
let args = null;
Expand Down Expand Up @@ -399,7 +491,16 @@ function compile(command, info, args, gdb) {
// if the platform is Windows, run start (which is a shell builtin, so
// we can't use child_process.spawn), which spawns a new instance of
// cmd to run the program
const file = getCompiledPath(info.dir, info.name);

// find the executable path
let file = "";

if (atom.config.get("gpp-compiler.compileDirectory") === "") {
file = getCompiledPath(info.dir, info.name);
} else {
file = getCompiledPath(atom.config.get("gpp-compiler.compileDirectory"), info.name);
}

const command = `start "${info.name}" cmd /C "${gdb ? "gdb" : ""} ${file} ${gdb ? "" : "& echo. & pause"}`;

debug("command", command);
Expand All @@ -408,8 +509,18 @@ function compile(command, info, args, gdb) {
// if the platform is mac, spawn open, which does the same thing as
// Windows' start, but is not a builtin, so we can child_process.spawn
// it

// find the executable path
let file = "";

if (atom.config.get("gpp-compiler.compileDirectory") === "") {
file = getCompiledPath(info.dir, info.name);
} else {
file = getCompiledPath(atom.config.get("gpp-compiler.compileDirectory"), info.name);
}

child_process.spawn("open", [
getCompiledPath(info.dir, info.name)
file
], options);
}
} else {
Expand Down
Loading