fe486c321d95b607f54068f9eb755b4e558b9a4d
2 # -*- coding: utf-8 -*-
7 Copyright (c) 2014 Frédéric Massart - FMCorz.net
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 http://github.com/FMCorz/mdk
29 import watchdog
.events
30 import watchdog
.observers
31 from ..command
import Command
32 from .. import js
, plugins
35 class JsCommand(Command
):
42 'help': 'the type of action to perform',
47 'help': 'keen to use shifter?'
56 'help': 'the name of the plugin or subsystem to target. If not passed, we do our best to guess from the current path.'
65 'help': 'the name of the module in the plugin or subsystem. If omitted all the modules will be shifted, except we are in a module.'
71 'action': 'store_true',
73 'help': 'watch for changes to re-shift'
80 'help': 'name of the instances',
89 'help': 'keen to generate documentation?'
96 'help': 'name of the instances',
107 _description
= 'Wrapper for JS functions'
110 if args
.mode
== 'shift':
112 elif args
.mode
== 'doc':
116 def shift(self
, args
):
119 Mlist
= self
.Wp
.resolveMultiple(args
.names
)
121 raise Exception('No instances to work on. Exiting...')
123 cwd
= os
.path
.realpath(os
.path
.abspath(os
.getcwd()))
124 mpath
= Mlist
[0].get('path')
125 relpath
= cwd
.replace(mpath
, '').strip('/')
127 # TODO Put that logic somewhere else because it is going to be re-used, I'm sure.
129 (subsystemOrPlugin
, pluginName
) = plugins
.PluginManager
.getSubsystemOrPluginFromPath(cwd
, Mlist
[0])
130 if subsystemOrPlugin
:
131 args
.plugin
= subsystemOrPlugin
+ ('_' + pluginName
) if pluginName
else ''
132 logging
.info("I guessed the plugin/subsystem to work on as '%s'" %
(args
.plugin
))
134 self
.argumentError('The argument --plugin is required, I could not guess it.')
139 while '/yui/src' in candidate
:
140 (head
, tail
) = os
.path
.split(candidate
)
141 if head
.endswith('/yui/src'):
148 logging
.info("I guessed the JS module to work on as '%s'" %
(args
.module
))
152 logging
.info('Let\'s shift everything you wanted on \'%s\'' %
(M
.get('identifier')))
155 processor
.shift(subsystemOrPlugin
=args
.plugin
, module
=args
.module
)
158 observer
= watchdog
.observers
.Observer()
162 processorArgs
= {'subsystemOrPlugin': args
.plugin
, 'module': args
.module
}
163 handler
= JsShiftWatcher(M
, processor
, processorArgs
)
164 observer
.schedule(handler
, processor
.getYUISrcPath(**processorArgs
), recursive
=True)
165 logging
.info('Watchdog set up on %s, waiting for changes...' %
(M
.get('identifier')))
172 except KeyboardInterrupt:
177 def document(self
, args
):
178 """The docmentation mode"""
180 Mlist
= self
.Wp
.resolveMultiple(args
.names
)
182 raise Exception('No instances to work on. Exiting...')
185 logging
.info('Documenting everything you wanted on \'%s\'. This may take a while...', M
.get('identifier'))
186 outdir
= self
.Wp
.getExtraDir(M
.get('identifier'), 'jsdoc')
187 outurl
= self
.Wp
.getUrl(M
.get('identifier'), extra
='jsdoc')
189 processor
.document(outdir
)
190 logging
.info('Documentation available at:\n %s\n %s', outdir
, outurl
)
193 class JsShiftWatcher(watchdog
.events
.FileSystemEventHandler
):
197 _ext
= ['.js', '.json']
200 def __init__(self
, M
, processor
, args
):
201 super(self
.__class__
, self
).__init__()
203 self
._processor
= processor
206 def on_modified(self
, event
):
209 def process(self
, event
):
210 if event
.is_directory
:
212 elif not os
.path
.splitext(event
.src_path
)[1] in self
._ext
:
215 logging
.info('[%s] (%s) Changes detected!' %
(self
._M
.get('identifier'), datetime
.datetime
.now().strftime('%H:%M:%S')))
218 self
._processor
.shift(**self
._args
)
219 except js
.ShifterCompileFailed
:
220 logging
.error(' /!\ Error: Compile failed!')