Ejemplo n.º 1
0
    def __init__(self, id, file, win_id, server_port):
        from haxe.complete import CompletionContext
        self.completion_context = CompletionContext()
        self.current_build = None
        self.selecting_build = False
        self.builds = []
        self.win_id = win_id
        
        self.server = hxserver.Server(server_port)

        
        self.project_file = file

        

        self.project_id = id
        if (self.project_file != None):
            self.project_path = os.path.normpath(os.path.dirname(self.project_file))
        else:
            self.project_path = None

        self.update_compiler_info()
Ejemplo n.º 2
0
class Project:
    def __init__(self, id, file, win_id, server_port):
        from haxe.complete import CompletionContext
        self.completion_context = CompletionContext()
        self.current_build = None
        self.selecting_build = False
        self.builds = []
        self.win_id = win_id
        
        self.server = hxserver.Server(server_port)

        
        self.project_file = file

        

        self.project_id = id
        if (self.project_file != None):
            self.project_path = os.path.normpath(os.path.dirname(self.project_file))
        else:
            self.project_path = None

        self.update_compiler_info()

    def project_dir (self, default):
        p = default
        if self.project_path != None:
            p = self.project_path
        return p

    def haxe_exec (self, view = None):
        haxe_exec = hxsettings.haxe_exec(view)
        if (haxe_exec != "haxe"):
            cwd = self.project_dir(".")
            haxe_exec = os.sep.join(os.path.join(cwd, hxsettings.haxe_exec(view)).split("/"))
        return haxe_exec

    def haxe_env (self,view = None):
        env = os.environ.copy()
       
       # should be project env not view env
        if view is not None :
            user_env = view.settings().get('build_env')
            if user_env:
                env.update(user_env)

        cwd = self.project_dir(".")
        libPath = hxsettings.haxe_library_path()
        if libPath != None :
            path = os.path.join(cwd, libPath)
            env["HAXE_LIBRARY_PATH"] = os.sep.join(path.split("/")).encode(sys.getfilesystemencoding())

        return env

    def haxe_build_env (self,view = None):
        
        cwd = self.project_dir(".")

        libPath = hxsettings.haxe_library_path()

        
        if libPath != None:
            path = os.path.join(cwd, )
            env = {
                "HAXE_LIBRARY_PATH" : os.sep.join(path.split("/")).encode(sys.getfilesystemencoding())
            }
        else:
            env = {}

        return env
    
    def start_server(self, view):
        cwd = self.project_dir(".")
        haxe_exec = self.haxe_exec(view)
        
        env = self.haxe_env(view)
        
        self.server.start(haxe_exec, cwd, env)
        

    def update_compiler_info (self):
        classes, packs, ver, std_paths = collect_compiler_info(self.project_path)

        #assume it's supported if no version available
        if ver is None:
            self.serverMode = True
        else:
            self.serverMode = int(ver.group(1)) >= 209

        self.std_paths = std_paths
        self.std_packages = packs
        self.std_classes = ["Void","String", "Float", "Int", "UInt", "Bool", "Dynamic", "Iterator", "Iterable", "ArrayAccess"]
        self.std_classes.extend(classes)

    def is_server_mode (self):
        return self.serverMode and hxsettings.get_bool('haxe-use-server-mode', True)

    def generate_build(self, view) :

        fn = view.file_name()

        if self.current_build is not None and fn == self.current_build.hxml and view.size() == 0 :  
            e = view.begin_edit()
            hxml_src = self.current_build.make_hxml()
            view.insert(e,0,hxml_src)
            view.end_edit(e)

    def select_build( self, view ) :
        scopes = view.scope_name(view.sel()[0].end()).split()
        
        if 'source.hxml' in scopes:
            view.run_command("save")

        self.extract_build_args( view , True )


    def extract_build_args( self, view = None , force_panel = False ) :
    
        self.builds = []

        if view is None:
            view = sublime.active_window().active_view()

        fn = view.file_name()

        if fn == None:
            return

        settings = view.settings()

        #log("filename: " + fn)
        
        folder = os.path.dirname(fn)
        win = view.window()
        if win is None:
            win = sublime.active_window()
        

        folders = win.folders()
        
        for f in folders:
            self.builds.extend(hxbuild.find_hxmls(f))
            self.builds.extend(hxbuild.find_nmmls(f))
                

        
        log( "num builds:" + str(len(self.builds)))

        # settings.set("haxe-complete-folder", folder)
        

        if len(self.builds) == 1:
            if force_panel : 
                sublime.status_message("There is only one build")

            # will open the build file
            #if force_panel :
            #   b = builds[0]
            #   f = b.hxml
            #   v = view.window().open_file(f,sublime.TRANSIENT) 

            self.set_current_build( view , int(0), force_panel )

        elif len(self.builds) == 0 and force_panel :
            sublime.status_message("No hxml or nmml file found")

            f = os.path.join(folder,"build.hxml")

            self.current_build = None
            self.get_build(view)
            self.current_build.hxml = f

            #for whatever reason generate_build doesn't work without transient
            win.open_file(f,sublime.TRANSIENT)

            self.set_current_build( view , int(0), force_panel )

        elif len(self.builds) > 1 and force_panel :
            buildsView = []
            for b in self.builds :
                #for a in b.args :
                #   v.append( " ".join(a) )
                buildsView.append( [b.to_string(), os.path.basename( b.hxml ) ] )

            self.selecting_build = True
            sublime.status_message("Please select your build")
            win.show_quick_panel( buildsView , lambda i : self.set_current_build(view, int(i), force_panel) , sublime.MONOSPACE_FONT )

        elif settings.has("haxe-build-id"):
            self.set_current_build( view , int(settings.get("haxe-build-id")), force_panel )
        
        else:
            self.set_current_build( view , int(0), force_panel )

    def set_current_build( self, view , id , force_panel ) :
        
        log( "set_current_build")
        if id < 0 or id >= len(self.builds) :
            id = 0
        
        view.settings().set( "haxe-build-id" , id ) 

        if len(self.builds) > 0 :
            self.current_build = self.builds[id]
            #log( "set_current_build - 2")
            hxpanel.default_panel().writeln( "building " + self.current_build.to_string() )
        else:
            hxpanel.default_panel().writeln( "No build found/selected" )
            
        self.selecting_build = False

        if force_panel and self.current_build is not None: # choose NME target
            if self.current_build.nmml is not None:
                sublime.status_message("Please select a NME target")
                nme_targets = []
                for t in hxbuild.HaxeBuild.nme_targets :
                    nme_targets.append( t[0] )

                view.window().show_quick_panel(nme_targets, lambda i : select_nme_target(self.current_build, i, view))

    def has_build (self):
        return self.current_build != None

    def run_build( self, view ) :
        
        haxe_exec = self.haxe_exec(view)
        env = self.haxe_env(view)
        if not self.has_build():
            self.extract_build_args(view)
        
        build = self.get_build(view)

        out, err = build.run(haxe_exec, env, self.serverMode, view, self)
        
        if (err != None and err != ""):
            msg = "build finished with errors"
            hxpanel.default_panel().writeln( msg)
            view.set_status( "haxe-status" , msg )
            hxpanel.default_panel().writeln(err)
            
        else:
            msg = "build finished successfull"
            view.set_status( "haxe-status" , msg )
            hxpanel.default_panel().writeln( msg )
        
    def run_sublime_build( self, view ) :
        

        if view is None: 
            view = sublime.active_window().active_view()

        log("start sublime build")


        haxe_exec = self.haxe_exec(view)
        env = self.haxe_build_env(view)
        
        if (self.has_build()):
            build = self.get_build(view)
        else:
            self.extract_build_args(view)
            build = self.get_build(view)

        cmd, build_folder, nekox_file_name = build.prepare_run(haxe_exec, self.serverMode, view, self)
        
        
        log(env)

        log(cmd)

        win = view.window()

        if win is None:
            win = sublime.active_window()
        
        win.run_command("haxe_exec", {
            "cmd": cmd,
            "working_dir": build_folder,
            "file_regex": haxe_file_regex,
            "env" : env
        })


    def clear_build( self ) :
        self.current_build = None
        self.completion_context.clear_completion()



    def __del__(self) :
        log( "kill server")
        self.server.stop()


    def get_build( self, view ) :
        
        if self.current_build is None and view.score_selector(0,"source.haxe.2") > 0 :

            fn = view.file_name()

            src_dir = os.path.dirname( fn )

            src = view.substr(sublime.Region(0, view.size()))
        
            build = hxbuild.HaxeBuild()
            build.target = "js"

            folder = os.path.dirname(fn)
            folders = view.window().folders()
            for f in folders:
                if f in fn :
                    folder = f

            pack = []
            for ps in hxsrctools.package_line.findall( src ) :
                if ps == "":
                    continue
                    
                pack = ps.split(".")
                for p in reversed(pack) : 
                    spl = os.path.split( src_dir )
                    if( spl[1] == p ) :
                        src_dir = spl[0]

            cl = os.path.basename(fn)
            cl = cl.encode('ascii','ignore')
            cl = cl[0:cl.rfind(".")]

            main = pack[0:]
            main.append( cl )
            build.main = ".".join( main )

            build.output = os.path.join(folder,build.main.lower() + ".js")

            log( "add cp: " + src_dir)

            build.args.append( ("-cp" , src_dir) )
            #build.args.append( ("-main" , build.main ) )

            build.args.append( ("-js" , build.output ) )
            #build.args.append( ("--no-output" , "-v" ) )

            build.hxml = os.path.join( src_dir , "build.hxml")
            
            #build.hxml = os.path.join( src_dir , "build.hxml")
            self.current_build = build
            
        return self.current_build.copy()