Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 337 lines (260 sloc) 11.682 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
import os
import sublime
import sublime_plugin
from threading import Thread

from sublime_haskell_common import log, get_cabal_project_dir_and_name_of_view, call_and_wait, get_setting, get_setting_async, set_setting, save_settings, get_haskell_command_window_view_file_project, attach_sandbox, output_error
from parseoutput import run_chain_build_thread
from autocomplete import autocompletion

OUTPUT_PANEL_NAME = "haskell_run_output"

cabal_tool = {
    True: {'command': 'cabal-dev', 'message': 'Cabal-Dev', 'extra': lambda cmd: attach_sandbox(cmd)},
    False: {'command': 'cabal', 'message': 'Cabal', 'extra': lambda cmd: cmd},
}

cabal_command = {
    'clean': {'steps': [['clean']], 'message': 'Cleaning'},
    'configure': {'steps': [['configure']], 'message': 'Configure'},
    'build': {'steps': [['build']], 'message': 'Building'},
    'typecheck': {'steps': [['build', '--ghc-options=-c']], 'message': 'Checking'},
    # Commands with warnings:
    # Run fast, incremental build first. Then build everything with -Wall and -fno-code
    # If the incremental build fails, the second step is not executed.
    'build_then_warnings': {'steps': [['build'], ['build', '--ghc-options=-fforce-recomp -Wall -fno-code']], 'message': 'Building'},
    'typecheck_then_warnings': {'steps': [['build', '--ghc-options=-c'], ['build', '--ghc-options=-fforce-recomp -Wall -fno-code']], 'message': 'Checking'},
    'rebuild': {'steps': [['clean'], ['configure'], ['build']], 'message': 'Rebuilding'},
    'install': {'steps': [['install']], 'message': 'Installing'}
}


# GLOBAL STATE

# Contains names of projects currently being built.
# To be updated only from the UI thread.
projects_being_built = set()


# Base command
class SublimeHaskellBaseCommand(sublime_plugin.WindowCommand):
    def is_enabled(self):
        return len(autocompletion.projects) > 0

    def build(self, command, use_cabal_dev=None):
        select_project(
            self.window,
            lambda n, d: run_build(self.window.active_view(), n, d, command, use_cabal_dev))


# Select project from list
# on_selected accepts name of project and directory of project
def select_project(window, on_selected):
    ps = autocompletion.projects.items()

    def run_selected(psel):
        on_selected(psel[0], psel[1]['dir'])

    if len(ps) == 0:
        return
    if len(ps) == 1: # There's only one project, build it
        run_selected(ps[0])
        return

    cabal_project_dir, cabal_project_name = get_cabal_project_dir_and_name_of_view(window.active_view())

    # Show current project first
    def compare(l, r):
        res = cmp(not (l[0] == cabal_project_name), not (r[0] == cabal_project_name))
        if res == 0:
            res = cmp(l[0], r[0])
        return res

    ps.sort(compare)

    def on_done(idx):
        if idx != -1:
            run_selected(ps[idx])

    window.show_quick_panel(map(lambda m: [m[0], m[1]['dir']], ps), on_done)


def run_build(view, project_name, project_dir, command, use_cabal_dev=None):
    global projects_being_built

    # Don't build if a build is already running for this project
    # We compare the project_name for simplicity (projects with same
    # names are of course possible, but unlikely, so we let them wait)
    if project_name in projects_being_built:
        print "SublimeHaskell: Not building '%s' because it is already being built" % project_name
        sublime.status_message('SublimeHaskell: Already building %s' % project_name)
        return
    # Set project as building
    projects_being_built.add(project_name)

    # Run cabal or cabal-dev
    if use_cabal_dev is None:
        use_cabal_dev = get_setting_async('use_cabal_dev')

    tool = cabal_tool[use_cabal_dev]
    config = cabal_command[command]

    # Title of tool: Cabal, Cabal-Dev
    tool_title = tool['message']
    # Title of action: Cleaning, Building, etc.
    action_title = config['message']
    # Extra arguments lambda
    extra_args = tool['extra']
    # Tool name: cabal, cabal-dev
    tool_name = tool['command']
    # Tool arguments (commands): build, clean, etc.
    tool_steps = config['steps']

    # Assemble command lines to run (possibly multiple steps)
    commands = [extra_args([tool_name] + step) for step in tool_steps]

    log('running build commands: {0}'.format(commands))

    def done_callback():
        # Set project as done being built so that it can be built again
        projects_being_built.remove(project_name)

    # Run them
    run_chain_build_thread(
        view,
        project_dir,
        tool_title + ': ' + action_title + ' ' + project_name,
        commands,
        on_done=done_callback)


class SublimeHaskellSwitchCabalDev(sublime_plugin.WindowCommand):
    def run(self):
        use_cabal_dev = get_setting('use_cabal_dev')
        sandbox = get_setting('cabal_dev_sandbox')
        sandboxes = get_setting('cabal_dev_sandbox_list')

        sandboxes.append(sandbox)
        sandboxes = list(set(sandboxes))
        set_setting('cabal_dev_sandbox_list', sandboxes)

        # No candboxes
        if len(sandboxes) == 0:
            sublime.status_message('SublimeHaskell: There is nothing to switch to')
            set_setting('use_cabal_dev', False)
            save_settings()
            return

        # One sandbox, just switch
        if len(sandboxes) == 1:
            set_setting('use_cabal_dev', not use_cabal_dev)
            if use_cabal_dev:
                now_using = 'Cabal'
            else:
                now_using = 'Cabal-Dev'
            sublime.status_message('SublimeHaskell: Switched to ' + now_using)
            save_settings()
            return

        # Many sandboxes, show list
        self.sorted_sands = sandboxes
        # Move previously used sandbox (or cabal) on top
        self.sorted_sands.remove(sandbox)
        if use_cabal_dev:
            self.sorted_sands.insert(0, sandbox)
            self.sorted_sands.insert(0, "<Cabal>")
        else:
            self.sorted_sands.insert(0, "<Cabal>")
            self.sorted_sands.insert(0, sandbox)

        self.window.show_quick_panel(self.sorted_sands, self.on_done)

    def on_done(self, idx):
        if idx == -1:
            return
        selected = self.sorted_sands[idx]
        if selected == "<Cabal>":
            set_setting('use_cabal_dev', False)
        else:
            set_setting('use_cabal_dev', True)
            set_setting('cabal_dev_sandbox', selected)

        save_settings()


# Default build system (cabal or cabal-dev)

class SublimeHaskellClean(SublimeHaskellBaseCommand):
    def run(self):
        self.build('clean')


class SublimeHaskellConfigure(SublimeHaskellBaseCommand):
    def run(self):
        self.build('configure')


class SublimeHaskellBuild(SublimeHaskellBaseCommand):
    def run(self):
        self.build('build_then_warnings')


class SublimeHaskellTypecheck(SublimeHaskellBaseCommand):
    def run(self):
        self.build('typecheck_then_warnings')


class SublimeHaskellRebuild(SublimeHaskellBaseCommand):
    def run(self):
        self.build('rebuild')


class SublimeHaskellInstall(SublimeHaskellBaseCommand):
    def run(self):
        self.build('install')


# Auto build current project
class SublimeHaskellBuildAuto(SublimeHaskellBaseCommand):
    def run(self):
        current_project_dir, current_project_name = get_cabal_project_dir_and_name_of_view(self.window.active_view())
        if current_project_name and current_project_dir:
            build_mode = get_setting('auto_build_mode')

            build_command = {
               'normal': 'build',
               'normal-then-warnings': 'build_then_warnings',
               'typecheck': 'typecheck',
               'typecheck-then-warnings': 'typecheck_then_warnings',
            }.get(build_mode)

            if not build_command:
                output_error(self.window, "SublimeHaskell: invalid auto_build_mode '%s'" % build_mode)

            run_build(self.window.active_view(), current_project_name, current_project_dir, build_command, None)


class SublimeHaskellRun(SublimeHaskellBaseCommand):
    def run(self):
        self.executables = []
        ps = []
        for p, info in autocompletion.projects.items():
            for e in info['executables']:
                ps.append((p + ": " + e['name'], {
                    'dir': info['dir'],
                    'name': e['name']
                }))

        # Nothing to run
        if len(ps) == 0:
            sublime.status_message('SublimeHaskell: Nothing to run')
            return

        cabal_project_dir, cabal_project_name = get_cabal_project_dir_and_name_of_view(self.window.active_view())

        # Show current project first
        def compare(l, r):
            res = cmp(not l[0].startswith(cabal_project_name), not r[0].startswith(cabal_project_name))
            if res == 0:
                res = cmp(l[0], r[0])
            return res

        ps.sort(compare)

        self.executables = map(lambda m: m[1], ps)
        self.window.show_quick_panel(map(lambda m: m[0], ps), self.on_done)

    def on_done(self, idx):
        if idx == -1:
            return
        selected = self.executables[idx]
        name = selected['name']
        base_dir = selected['dir']
        bin_file = os.path.join(selected['dir'], 'dist', 'build', name, name)

        hide_output(self.window)

        sublime.status_message('SublimeHaskell: Running ' + name + "...")

        # Run in thread
        thread = Thread(
            target=run_binary,
            args=(name, bin_file, base_dir))
        thread.start()


def run_binary(name, bin_file, base_dir):
    exit_code, out, err = call_and_wait(bin_file, cwd=base_dir)
    window = sublime.active_window()
    if not window:
        return
    if exit_code == 0:
        sublime.set_timeout(lambda: sublime.status_message('SublimeHaskell: Running ' + name + u" \u2714"), 0)
        sublime.set_timeout(lambda: write_output(window, out, base_dir), 0)
    else:
        sublime.set_timeout(lambda: sublime.status_message('SublimeHaskell: Running ' + name + u" \u2717"), 0)
        sublime.set_timeout(lambda: write_output(window, err, base_dir), 0)


def write_output(window, text, base_dir):
    "Write text to Sublime's output panel."
    output_view = window.get_output_panel(OUTPUT_PANEL_NAME)
    output_view.set_read_only(False)
    # Configure Sublime's error message parsing:
    output_view.settings().set("result_base_dir", base_dir)
    # Write to the output buffer:
    edit = output_view.begin_edit()
    output_view.insert(edit, 0, text)
    output_view.end_edit(edit)
    # Set the selection to the beginning of the view so that "next result" works:
    output_view.sel().clear()
    output_view.sel().add(sublime.Region(0))
    output_view.set_read_only(True)
    # Show the results panel:
    window.run_command('show_panel', {'panel': 'output.' + OUTPUT_PANEL_NAME})


def hide_output(window):
    window.run_command('hide_panel', {'panel': 'output.' + OUTPUT_PANEL_NAME})

def run_build_commands_with(msg, cmds):
    """Run general build commands"""
    window, view, file_shown_in_view = get_haskell_command_window_view_file_project()
    if not file_shown_in_view:
        return
    syntax_file_for_view = view.settings().get('syntax').lower()
    if 'haskell' not in syntax_file_for_view:
        return
    cabal_project_dir, cabal_project_name = get_cabal_project_dir_and_name_of_view(view)
    if not cabal_project_dir:
        return

    run_chain_build_thread(view, cabal_project_dir, msg(cabal_project_name), cmds)

def run_build_command_with(msg, cmd):
    """Run one command"""
    run_build_commands_with(msg, [cmd])
Something went wrong with that request. Please try again.