Skip to content

Commit

Permalink
1. default rules set to @per; #181
Browse files Browse the repository at this point in the history
3. a new setting for setting a tmp dir;  #189 #187

if no tmp dir setted, will try `system tmp dir` and `current user home dir`
  • Loading branch information
junstyle committed Sep 9, 2022
1 parent 454ef35 commit 870e5c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17010,6 +17010,7 @@ function resolveRun(exitCode, stdout, stderr) {

// src/index.ts
var TmpDir = os.tmpdir();
var HomeDir = os.homedir();
var isRunning = false;
var PHPCSFixer = class extends PHPCSFixerConfig {
constructor() {
Expand Down Expand Up @@ -17037,6 +17038,7 @@ var PHPCSFixer = class extends PHPCSFixerConfig {
this.allowRisky = config.get("allowRisky", false);
this.pathMode = config.get("pathMode", "override");
this.exclude = config.get("exclude", []);
this.tmpDir = config.get("tmpDir", "");
if (this.executablePath.endsWith(".phar")) {
this.pharPath = this.executablePath.replace(/^php[^ ]* /i, "");
this.executablePath = import_vscode2.workspace.getConfiguration("php").get("validate.executablePath", "php");
Expand Down Expand Up @@ -17127,8 +17129,22 @@ var PHPCSFixer = class extends PHPCSFixerConfig {
if (isPartial) {
filePath = TmpDir + "/php-cs-fixer-partial.php";
} else {
filePath = path.join(TmpDir, "php-cs-fixer", "tmp" + Math.random(), uri.fsPath.replace(/^.*[\\/]/, ""));
fs.mkdirSync(path.dirname(filePath), { recursive: true });
let tmpDirs = [this.tmpDir, TmpDir, HomeDir].filter(Boolean);
for (let i = 0; i < tmpDirs.length; i++) {
filePath = path.join(tmpDirs[i], "pcf-tmp" + Math.random(), uri.fsPath.replace(/^.*[\\/]/, ""));
try {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
this.tmpDir = tmpDirs[i];
break;
} catch (err) {
console.error(err);
filePath = "";
}
}
if (!filePath) {
statusInfo("can't make tmp dir, please check the php-cs-fixer settings, set a writable dir to tmpDir.");
return Promise.reject();
}
}
fs.writeFileSync(filePath, text);
const args = this.getArgs(uri, filePath);
Expand Down Expand Up @@ -17175,7 +17191,7 @@ var PHPCSFixer = class extends PHPCSFixerConfig {
}
}).finally(() => {
isRunning = false;
if (!isDiff) {
if (!isDiff && !isPartial) {
fs.rm(path.dirname(filePath), { recursive: true, force: true }, function(err) {
err && console.error(err);
});
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "php-cs-fixer",
"displayName": "php cs fixer",
"description": "PHP CS Fixer extension for VS Code, php formatter, php code beautify tool, format html",
"version": "0.3.6",
"version": "0.3.7",
"publisher": "junstyle",
"author": "junstyle",
"license": "ISC",
Expand Down Expand Up @@ -64,8 +64,8 @@
"string",
"object"
],
"default": "@PSR12",
"description": "PHP CS Fixer level setting (@PSR1, @PSR2, @PSR12, @Symfony). Support json formatted value. @see: https://github.com/FriendsOfPHP/PHP-CS-Fixer"
"default": "@PER",
"description": "PHP CS Fixer rules (such as @PSR1, @PSR2, @PSR12, @PER, @Symfony...). Support json formatted value. @see: https://github.com/FriendsOfPHP/PHP-CS-Fixer"
},
"php-cs-fixer.config": {
"type": "string",
Expand Down Expand Up @@ -116,6 +116,11 @@
"default": true,
"description": "register php document formatting provider, right mouse-click context menu, show as 'Format Document', after changing this option you should restart your editor."
},
"php-cs-fixer.tmpDir": {
"type": "string",
"default": "",
"description": "the dir for tmp files, make sure you have 'writable' permission on this dir."
},
"php-cs-fixer.lastDownload": {
"type": "integer",
"default": 1,
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export class PHPCSFixerConfig {
editorFormatOnSave: boolean
// fileAutoSave: boolean
// fileAutoSaveDelay: number
tmpDir: string
}
22 changes: 19 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PHPCSFixerConfig } from './index.d'
import { clearOutput, disposeOutput, hideStatusBar, output, showOutput, statusInfo } from './output'
import { runAsync } from './runAsync'
const TmpDir = os.tmpdir()
const HomeDir = os.homedir()
let isRunning = false
let lastActiveEditor = null

Expand Down Expand Up @@ -39,6 +40,7 @@ class PHPCSFixer extends PHPCSFixerConfig {
this.allowRisky = config.get('allowRisky', false)
this.pathMode = config.get('pathMode', 'override')
this.exclude = config.get('exclude', [])
this.tmpDir = config.get('tmpDir', '')

if (this.executablePath.endsWith('.phar')) {
this.pharPath = this.executablePath.replace(/^php[^ ]* /i, '')
Expand Down Expand Up @@ -177,8 +179,22 @@ class PHPCSFixer extends PHPCSFixerConfig {
if (isPartial) {
filePath = TmpDir + '/php-cs-fixer-partial.php'
} else {
filePath = path.join(TmpDir, 'php-cs-fixer', 'tmp' + Math.random(), uri.fsPath.replace(/^.*[\\/]/, ''))
fs.mkdirSync(path.dirname(filePath), { recursive: true })
let tmpDirs = [this.tmpDir, TmpDir, HomeDir].filter(Boolean);
for (let i = 0; i < tmpDirs.length; i++) {
filePath = path.join(tmpDirs[i], 'pcf-tmp' + Math.random(), uri.fsPath.replace(/^.*[\\/]/, ''))
try {
fs.mkdirSync(path.dirname(filePath), { recursive: true })
this.tmpDir = tmpDirs[i]
break;
} catch (err) {
console.error(err)
filePath = ''
}
}
if (!filePath) {
statusInfo("can't make tmp dir, please check the php-cs-fixer settings, set a writable dir to tmpDir.")
return Promise.reject()
}
}

fs.writeFileSync(filePath, text)
Expand Down Expand Up @@ -233,7 +249,7 @@ class PHPCSFixer extends PHPCSFixerConfig {
})
.finally(() => {
isRunning = false
if (!isDiff) {
if (!isDiff && !isPartial) {
fs.rm(path.dirname(filePath), { recursive: true, force: true }, function (err) { err && console.error(err) })
}
})
Expand Down

0 comments on commit 870e5c0

Please sign in to comment.