Exemple #1
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)
Exemple #2
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 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 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)
Exemple #5
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)