Example #1
0
 def is_enabled(self):
     # TODO(guillermooo): Fix this in pub_package.DartFile
     view = self.window.active_view()
     if not view:
         return False
     dart_view = DartFile(view)
     return (not dart_view.is_web_app) and dart_view.is_server_app
Example #2
0
    def run(self, action='primary', force_update=False, kill_only=False):
        '''
        @action
          One of: primary, secondary
        '''

        self.raise_event(self, EventSource.ON_DART_RUN)

        try:
            view = self.window.active_view()
        except TypeError:
            return

        if force_update or DartSmartRunCommand.last_run_file[0] is None:
            try:
                DartSmartRunCommand.last_run_file = (
                                                DartFile(view).is_pubspec,
                                                view.file_name())
            except TypeError:
                return

        if DartSmartRunCommand.last_run_file[0]:
            self.window.run_command('dart_run_pubspec', {
                'action': action,
                'file_name': DartSmartRunCommand.last_run_file[1]
                })
            return

        self.window.run_command('dart_run_file', {
            'action': action,
            'file_name': DartSmartRunCommand.last_run_file[1],
            'kill_only': kill_only,
            })
 def test_is_pubspec_CanSucceed(self):
     with TemporaryDirectory() as d:
         fname = os.path.join(d, 'pubspec.yaml')
         with open(fname, 'w'):
             pass
         with self.open_file(fname) as view:
             df = DartFile(view)
             self.assertTrue(df.is_pubspec)
 def test_is_example_CanSucceed(self):
     _, tmp_dir = make_package(dirs=['example'])
     fname = os.path.join(tmp_dir.name, 'example', 'foo.dart')
     open(fname, 'w').close()
     with self.open_file(fname) as view:
         df = DartFile(view)
         self.assertTrue(df.is_example)
     tmp_dir.cleanup()
 def test_is_example_FailsWhenExpected(self):
     _, tmp_dir = make_package(dirs=['bin'])
     fname = os.path.join(tmp_dir.name, 'bin', 'foo.dart')
     open(fname, 'w').close()
     with self.open_file(fname) as view:
         df = DartFile(view)
         self.assertFalse(df.is_example)
     tmp_dir.cleanup()
 def test_is_web_app_FailsIfNoPubPackageAvailable(self):
     _, tmp_dir = make_package(pubspec=False, dirs=['web'])
     fname = os.path.join(tmp_dir.name, 'web', 'foo.dart')
     open(fname, 'w').close()
     with self.open_file(fname) as view:
         df = DartFile(view)
         self.assertFalse(df.is_web_app)
     tmp_dir.cleanup()
 def test_is_web_app_ReturnsTrueIfFileUnderExample(self):
     _, tmp_dir = make_package(pubspec=True, dirs=['example'])
     fname = os.path.join(tmp_dir.name, 'example', 'foo.dart')
     open(fname, 'w').close()
     with self.open_file(fname) as view:
         df = DartFile(view)
         self.assertTrue(df.is_web_app)
     tmp_dir.cleanup()
 def test_is_web_app_ReturnsFalseIfWebDirMissing(self):
     _, tmp_dir = make_package(pubspec=True, dirs=['example', 'bin'])
     fname = os.path.join(tmp_dir.name, 'bin', 'foo.dart')
     f = open(fname, 'w').close()
     with self.open_file(fname) as view:
         df = DartFile(view)
         self.assertFalse(df.is_web_app)
     tmp_dir.cleanup()
Example #9
0
    def run(self, file_name=None, action='primary', kill_only=False):
        '''
        @action
          One of: primary, secondary

        @kill_only
          Whether we should simply kill any running processes.
        '''
        assert kill_only or (file_name and not kill_only), 'wrong call'

        # First, clean up any existing prosesses.
        if DartRunFileCommand.is_server_running:
            self.execute(kill=True)
            self.pub_serve.stop()
            DartRunFileCommand.is_server_running = False
            if self.panel:
                self.panel.write('[pub serve stopped]\n')

        self.stop_server_observatory()

        if kill_only:
            self.window.run_command("dart_exec", {"kill": True})
            DartRunFileCommand.is_script_running = False
            return

        try:
            working_dir = os.path.dirname(find_pubspec(file_name))
        except:
            try:
                if not working_dir:
                    working_dir = os.path.dirname(file_name)
            except:
                _logger.debug('cannot run an unsaved file')
                return

        dart_view = DartFile.from_path(file_name)

        if dart_view.is_server_app:
            self.run_server_app(file_name, working_dir, action)
            return

        if dart_view.is_web_app:
            self.run_web_app(dart_view, working_dir, action)
            return

        # At this point, we are looking at a file that either:
        #   - is not a .dart or .html file
        #   - is outside of a pub package
        # As a last restort, run the file as a script, but only if the user
        # requested a 'secondary' action.
        if action != 'secondary' or not dart_view.is_dart_file:
            print("Dart: Cannot determine best action for {}".format(
                dart_view.path))
            _logger.debug("cannot determine best run action for %s",
                          dart_view.path)
            return

        self.run_server_app(file_name, working_dir, action)
Example #10
0
    def on_query_context(self, view, key, operator, operand, match_all):
        if key == 'dart_can_do_launch':
            value = DartFile(view).is_runnable
            return self._check(value, operator, operand, match_all)

        if key == 'dart_can_do_relaunch':
            value = (DartFile(view).is_runnable or
                     DartSmartRunCommand.last_run_file[1])
            return self._check(value, operator, operand, match_all)

        if key == 'dart_can_show_observatory':
            value = DartRunFileCommand.observatory != None
            return self._check(value, operator, operand, match_all)

        if key == 'dart_services_running':
            value = any((DartRunFileCommand.observatory != None,
                        DartRunFileCommand.is_server_running,
                        DartRunFileCommand.is_script_running))
            return self._check(value, operator, operand, match_all)
Example #11
0
    def start_default_browser(self, file_name):
        sdk = SDK()

        if not sdk.path_to_default_user_browser:
            _logger.info('no default user browser defined')
            print("Dart: No default user browser defined "
                  "in Dart plugin settings")
            return

        dart_view = DartFile.from_path(file_name)
        url = 'http://localhost:8080'
        if dart_view.url_path:
            url = url + "/" + dart_view.url_path

        # TODO(guillermooo): make GUIProcess wrapper to abstract out some of
        # the stuff below?
        if sublime.platform() == 'osx':
            bin_ = GenericBinary('open', sdk.path_to_default_user_browser)
            after(1000, lambda: bin_.start(args=[url]))
            return

        elif sublime.platform() == 'windows':
            # FIXME(guillermooo): On Windows, Firefox won't work when started
            # from the cmdline only. If it's started first from the shell, it
            # will work here as well.
            path = sdk.path_to_default_user_browser
            bin_ = GenericBinary(path)
            after(
                1000, lambda: bin_.start(
                    args=[url],
                    shell=True,
                    cwd=os.path.dirname(path),
                ))
            return

        path = sdk.path_to_default_user_browser
        bin_ = GenericBinary(path)
        after(
            1000, lambda: bin_.start(
                args=[url],
                shell=True,
                cwd=os.path.dirname(path),
            ))
Example #12
0
    def start_default_browser(self, file_name):
        sdk = SDK()

        if not sdk.path_to_default_user_browser:
            _logger.info('no default user browser defined')
            print("Dart: No default user browser defined "
                  "in Dart plugin settings")
            return

        dart_view = DartFile.from_path(file_name)
        url = 'http://localhost:8080'
        if dart_view.url_path:
            url = url + "/" + dart_view.url_path

        # TODO(guillermooo): make GUIProcess wrapper to abstract out some of
        # the stuff below?
        if sublime.platform() == 'osx':
            bin_ = GenericBinary('open', sdk.path_to_default_user_browser)
            after(1000, lambda: bin_.start(args=[url]))
            return

        elif sublime.platform() == 'windows':
            # FIXME(guillermooo): On Windows, Firefox won't work when started
            # from the cmdline only. If it's started first from the shell, it
            # will work here as well.
            path = sdk.path_to_default_user_browser
            bin_ = GenericBinary(path)
            after(1000, lambda: bin_.start(
                                   args=[url],
                                   shell=True,
                                   cwd=os.path.dirname(path),
                                   ))
            return

        path = sdk.path_to_default_user_browser
        bin_ = GenericBinary(path)
        after(1000, lambda: bin_.start(
                               args=[url],
                               shell=True,
                               cwd=os.path.dirname(path),
                               ))
 def test_is_dart_file_CanSucceed(self):
     f = NamedTemporaryFile(suffix='.dart')
     with self.open_file(f.name) as view:
         df = DartFile(view)
         self.assertTrue(df.is_dart_file)
     f.close()
Example #14
0
 def check(self, view):
     return DartFile(view).is_dart_file
Example #15
0
    def run(self, file_name=None, action='primary', kill_only=False):
        '''
        @action
          One of: primary, secondary

        @kill_only
          Whether we should simply kill any running processes.
        '''
        assert kill_only or (file_name and not kill_only), 'wrong call'

        # First, clean up any existing prosesses.
        if DartRunFileCommand.is_server_running:
            self.execute(kill=True)
            self.pub_serve.stop()
            DartRunFileCommand.is_server_running = False
            if self.panel:
                self.panel.write('[pub serve stopped]\n')

        self.stop_server_observatory()

        if kill_only:
            self.window.run_command("dart_exec", {
                "kill": True
                })
            DartRunFileCommand.is_script_running = False
            return

        try:
            working_dir = os.path.dirname(find_pubspec(file_name))
        except:
            try:
                if not working_dir:
                    working_dir = os.path.dirname(file_name)
            except:
                _logger.debug('cannot run an unsaved file')
                return

        dart_view = DartFile.from_path(file_name)

        if dart_view.is_server_app:
            self.run_server_app(file_name, working_dir, action)
            return

        if dart_view.is_web_app:
            self.run_web_app(dart_view, working_dir, action)
            return

        # At this point, we are looking at a file that either:
        #   - is not a .dart or .html file
        #   - is outside of a pub package
        # As a last restort, run the file as a script, but only if the user
        # requested a 'secondary' action.
        if action != 'secondary' or not dart_view.is_dart_file:
            print("Dart: Cannot determine best action for {}".format(
                  dart_view.path
                  ))
            _logger.debug("cannot determine best run action for %s",
                          dart_view.path)
            return

        self.run_server_app(file_name, working_dir, action)
 def check(self, view):
     # Offer Dart completions in Dart files when the caret isn't in a
     # string or comment. If strings or comments, offer plain Sublime Text
     # completions.
     return (not self._in_string_or_comment(view)
             and DartFile(view).is_dart_file)
Example #17
0
    def run(self, file_name=None, action='primary', kill_only=False):
        '''
        @action
          One of: [primary, secondary]

        @kill_only
          If `True`, simply kill any running processes we've started.
        '''
        assert kill_only or file_name, 'wrong call'

        self._cleanup()

        if kill_only:
            self._kill()
            return

        working_dir = None
        try:
            working_dir = os.path.dirname(find_pubspec(file_name))
        except (TypeError, AttributeError):
            try:
                if not working_dir:
                    working_dir = os.path.dirname(file_name)
            except TypeError as e:
                _logger.debug('cannot run an unsaved file')
                _logger.debug(e)
                return
            except Exception as e:
                _logger.error('programmer error: this exception needs to be handled')
                _logger.error(e)
                return
        except Exception as e:
            _logger.error('programmer error: this exception needs to be handled')
            _logger.error(e)
            return

        dart_view = DartFile.from_path(file_name)

        if dart_view.is_server_app:
            self.run_server_app(file_name, working_dir, action)
            return

        if dart_view.is_web_app:
            self.run_web_app(dart_view, working_dir, action)
            return

        # TODO: improve detection of runnable file (for example, don't attempt
        # to run a part of a library).
        # At this point, we are looking at a file that either:
        #   - is not a .dart or .html file
        #   - is outside of a pub package
        # As a last resort, try to run the file as a script.
        if action != 'primary' or not dart_view.is_dart_file:
            print("Dart: Cannot determine best action for {}".format(
                  dart_view.path
                  ))
            _logger.debug("cannot determine best run action for %s",
                          dart_view.path)
            return

        self.run_server_app(file_name, working_dir, action)
 def testCanInit(self):
     df = DartFile(self.v)
     self.assertEqual(df.view, self.v)
 def test_has_prefix_RaisesAssertionErrorIfCalledWithNone(self):
     df = DartFile(self.v)
     self.assertRaises(AssertionError, df.has_prefix, None)
 def test_has_prefix_FailsWhenExpected(self):
     f = NamedTemporaryFile(suffix='.dart')
     with self.open_file(f.name) as view:
         df = DartFile(view)
         self.assertFalse(df.has_prefix('?xxx?'))
     f.close()
 def test_is_pubspec_FailsWhenExpected(self):
     f = NamedTemporaryFile(suffix='.js', delete=True)
     with self.open_file(f.name) as view:
         df = DartFile(view)
         self.assertFalse(df.is_pubspec)
     f.close()
 def test_has_prefix_CanSucceed(self):
     f = NamedTemporaryFile(suffix='.dart')
     with self.open_file(f.name) as view:
         df = DartFile(view)
         self.assertTrue(df.has_prefix(os.path.dirname(f.name)))
     f.close()