This repository was archived by the owner on Jan 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathphp-cs-fixer.coffee
More file actions
237 lines (202 loc) · 7.57 KB
/
Copy pathphp-cs-fixer.coffee
File metadata and controls
237 lines (202 loc) · 7.57 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
{CompositeDisposable} = require 'atom'
{BufferedProcess} = require 'atom'
fs = require 'fs'
path = require 'path'
module.exports = PhpCsFixer =
subscriptions: null
config:
phpExecutablePath:
title: 'PHP executable path'
type: 'string'
default: 'php'
description: 'The path to the `php` executable.'
order: 10
phpArguments:
title: 'Add PHP arguments'
type: 'array'
default: []
description: 'Add arguments, like for example
`-n`, to the PHP executable.'
order: 11
executablePath:
title: 'PHP-CS-fixer executable path'
type: 'string'
default: 'php-cs-fixer'
description: 'The path to the `php-cs-fixer` executable.'
order: 20
rules:
title: 'PHP-CS-Fixer Rules'
type: 'string'
default: '@PSR2'
description: 'A list of rules (based on php-cs-fixer 2.0),
for example: `@PSR2,no_short_echo_tag,indentation_type`.
See <https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage>
for a complete list.
Will be ignored if a config file is used.'
order: 21
allowRisky:
title: 'Allow risky'
type: 'boolean'
default: false
description: 'Option allows you to set whether risky rules may run.
Will be ignored if a config file is used.'
order: 22
pathMode:
title: 'PHP-CS-Fixer Path-Mode'
type: 'string'
default: 'override'
enum: ['override', 'intersection']
description: 'Specify path mode (can be override or intersection).'
order: 23
fixerArguments:
title: 'PHP-CS-Fixer arguments'
type: 'array'
default: ['--using-cache=no', '--no-interaction']
description: 'Add arguments, like for example `--using-cache=false`,
to the PHP-CS-Fixer executable. Run `php-cs-fixer help fix`
in your command line,
to get a full list of all supported arguments.'
order: 24
configPath:
title: 'PHP-CS-fixer config file path'
type: 'string'
default: ''
description: 'Optionally provide the path to the `.php_cs` config file,
if the path is not provided it will be loaded from the root
path of the current project.'
order: 25
executeOnSave:
title: 'Execute on save'
type: 'boolean'
default: false
description: 'Execute PHP CS fixer on save'
order: 30
showInfoNotifications:
title: 'Show notifications'
type: 'boolean'
default: false
description: 'Show some status informations from the last "fix".'
order: 31
activate: (state) ->
atom.config.observe 'php-cs-fixer.executeOnSave', =>
@executeOnSave = atom.config.get 'php-cs-fixer.executeOnSave'
atom.config.observe 'php-cs-fixer.phpExecutablePath', =>
@phpExecutablePath = atom.config.get 'php-cs-fixer.phpExecutablePath'
atom.config.observe 'php-cs-fixer.executablePath', =>
@executablePath = atom.config.get 'php-cs-fixer.executablePath'
atom.config.observe 'php-cs-fixer.configPath', =>
@configPath = atom.config.get 'php-cs-fixer.configPath'
atom.config.observe 'php-cs-fixer.allowRisky', =>
@allowRisky = atom.config.get 'php-cs-fixer.allowRisky'
atom.config.observe 'php-cs-fixer.rules', =>
@rules = atom.config.get 'php-cs-fixer.rules'
atom.config.observe 'php-cs-fixer.showInfoNotifications', =>
@showInfoNotifications = atom
.config.get 'php-cs-fixer.showInfoNotifications'
atom.config.observe 'php-cs-fixer.phpArguments', =>
@phpArguments = atom.config.get 'php-cs-fixer.phpArguments'
atom.config.observe 'php-cs-fixer.fixerArguments', =>
@fixerArguments = atom.config.get 'php-cs-fixer.fixerArguments'
atom.config.observe 'php-cs-fixer.pathMode', =>
@pathMode = atom.config.get 'php-cs-fixer.pathMode'
# Events subscribed to in atom's system can be easily cleaned up
# with a CompositeDisposable
@subscriptions = new CompositeDisposable
# Register command that toggles this view
fixer = 'php-cs-fixer:fix': => @fix()
@subscriptions.add atom.commands.add 'atom-workspace', fixer
# Add workspace observer and save handler
@subscriptions.add atom.workspace.observeTextEditors (editor) =>
@subscriptions.add editor.getBuffer().onWillSave =>
if editor.getGrammar().name == "PHP" and @executeOnSave
@fix()
deactivate: ->
@subscriptions.dispose()
fix: ->
editor = atom.workspace.getActiveTextEditor()
filePath = editor.getPath() if editor && editor.getPath
command = @phpExecutablePath
args = []
if @phpArguments.length
if @phpArguments.length > 1
args = @phpArguments
else
args = @phpArguments[0].split(' ')
fileSearchStartPath = path.dirname(filePath.toString())
unless @executablePath and @fileExists(@executablePath)
vendorBinary = 'vendor/bin/php-cs-fixer'
@executablePath = @findFile(fileSearchStartPath, vendorBinary)
args = args.concat [@executablePath, 'fix', filePath]
configFiles = ['.php_cs', '.php_cs.dist']
unless @configPath
@configPath = @findFile(fileSearchStartPath, configFiles)
if @configPath
args.push '--config=' + @configPath
# add optional options
if not @configPath
args.push '--allow-risky=yes' if @allowRisky
args.push '--rules=' + @rules if @rules
args.push '--path-mode=' + @pathMode if @pathMode
if @fixerArguments.length and not @configPath
if @fixerArguments.length > 1
fixerArgs = @fixerArguments
else
fixerArgs = @fixerArguments[0].split(' ')
args = args.concat fixerArgs
stdout = (output) ->
if PhpCsFixer.showInfoNotifications
if (/^\s*\d*[)]/.test(output))
atom.notifications.addSuccess(output)
else
atom.notifications.addInfo(output)
console.log(output)
stderr = (output) ->
if PhpCsFixer.showInfoNotifications
if (output.replace(/\s/g,"") == "")
# do nothing
# temporary fixing:
# https://github.com/pfefferle/atom-php-cs-fixer/issues/35
else if (/^Loaded config/.test(output))
atom.notifications.addInfo(output)
else
atom.notifications.addError(output)
console.error(output)
exit = (code) -> console.log("#{command} exited with code: #{code}")
process = new BufferedProcess({
command: command,
args: args,
stdout: stdout,
stderr: stderr,
exit: exit
}) if filePath
# copied from the AtomLinter li
# see:
# https://github.com/AtomLinter/atom-linter/blob/master/lib/helpers.coffee#L112
#
# The AtomLinter is licensed under "The MIT License (MIT)"
#
# Copyright (c) 2015 AtomLinter
#
# See the full license here:
# https://github.com/AtomLinter/atom-linter/blob/master/LICENSE
findFile: (startDir, names) ->
throw new Error "Specify a filename to find" unless arguments.length
unless names instanceof Array
names = [names]
startDir = startDir.split(path.sep)
while startDir.length
currentDir = startDir.join(path.sep)
for name in names
filePath = path.join(currentDir, name)
try
console.log("!!!Looking in #{filePath}")
fs.accessSync(filePath, fs.R_OK)
return filePath
startDir.pop()
return null
fileExists: (path) ->
try
fs.accessSync(filePath, fs.R_OK)
return true
catch
return false