|
| 1 | +let s:antXmlTemplate = [ |
| 2 | + \ ' <target name="vjc-test-conditions">', |
| 3 | + \ ' <condition property="vjc-netbeans-condition">', |
| 4 | + \ ' <isset property="javac.classpath" />', |
| 5 | + \ ' </condition>', |
| 6 | + \ ' <condition property="vjc-project-condition">', |
| 7 | + \ ' <isreference refid="project.classpath"/>', |
| 8 | + \ ' </condition>', |
| 9 | + \ ' <condition property="vjc-classpath-condition">', |
| 10 | + \ ' <isreference refid="classpath"/>', |
| 11 | + \ ' </condition>', |
| 12 | + \ ' </target>', |
| 13 | + \ ' <target name="vjc-netbeans-classpath" depends="vjc-test-conditions" if="vjc-netbeans-condition">', |
| 14 | + \ ' <property name="javavi.classpath" value="${javac.classpath}" />', |
| 15 | + \ ' </target>', |
| 16 | + \ ' <target name="vjc-project-classpath" depends="vjc-test-conditions" if="vjc-project-condition">', |
| 17 | + \ ' <property name="javavi.classpath" refid="project.classpath"/>', |
| 18 | + \ ' </target>', |
| 19 | + \ ' <target name="vjc-classpath" depends="vjc-test-conditions" if="vjc-classpath-condition">', |
| 20 | + \ ' <property name="javavi.classpath" refid="project.classpath"/>', |
| 21 | + \ ' </target>', |
| 22 | + \ ' <target name="vjc-printclasspath" depends="vjc-project-classpath,vjc-netbeans-classpath,vjc-classpath">', |
| 23 | + \ ' <echo message="${javavi.classpath}"/>', |
| 24 | + \ ' </target>'] |
| 25 | + |
| 26 | +function! s:Log(log) |
| 27 | + let log = type(a:log) == type("") ? a:log : string(a:log) |
| 28 | + call javacomplete#logger#Log("[classpath.ant] ". log) |
| 29 | +endfunction |
| 30 | + |
| 31 | +function! javacomplete#classpath#ant#IfAnt() |
| 32 | + if executable('ant') && g:JavaComplete_AntPath != "" |
| 33 | + return 1 |
| 34 | + endif |
| 35 | + return 0 |
| 36 | +endfunction |
| 37 | + |
| 38 | +function! javacomplete#classpath#ant#Generate(force) abort |
| 39 | + let g:JavaComplete_ProjectKey = substitute(g:JavaComplete_AntPath, '[\\/:;.]', '_', 'g') |
| 40 | + let path = javacomplete#util#GetBase("classpath". g:FILE_SEP). g:JavaComplete_ProjectKey |
| 41 | + |
| 42 | + if filereadable(path) |
| 43 | + if a:force == 0 && getftime(path) >= getftime(g:JavaComplete_AntPath) |
| 44 | + call s:Log("get libs from cache file") |
| 45 | + return join(readfile(path), '') |
| 46 | + endif |
| 47 | + call javacomplete#util#RemoveFile(javacomplete#util#GetBase('cache'). g:FILE_SEP. 'class_packages_'. g:JavaComplete_ProjectKey. '.dat') |
| 48 | + endif |
| 49 | + |
| 50 | + let s:antPath = path |
| 51 | + let s:antOutput = [] |
| 52 | + let cmd = "ant -projecthelp -v | grep '^ init\\>'" |
| 53 | + call javacomplete#util#RunSystem( |
| 54 | + \ cmd, "ant check 'init' target process", |
| 55 | + \ "javacomplete#classpath#ant#CheckInitTargetHandler") |
| 56 | + return '.' |
| 57 | +endfunction |
| 58 | + |
| 59 | +function! javacomplete#classpath#ant#CheckInitTargetHandler(data, event) |
| 60 | + if a:event == 'exit' |
| 61 | + if a:data == "0" |
| 62 | + let hasInitTarget = !empty(s:antOutput) |
| 63 | + let s:antOutput = [] |
| 64 | + call s:BuildAntClasspath(hasInitTarget) |
| 65 | + else |
| 66 | + echohl WarningMsg | echomsg "Failed to check 'init' target" | echohl None |
| 67 | + endif |
| 68 | + elseif a:event == 'stdout' |
| 69 | + for data in filter(a:data,'v:val =~ "^ init\\>.*$"') |
| 70 | + if g:JavaComplete_ShowExternalCommandsOutput |
| 71 | + echomsg data |
| 72 | + endif |
| 73 | + if exists('s:antOutput') |
| 74 | + call add(s:antOutput, data) |
| 75 | + endif |
| 76 | + endfor |
| 77 | + elseif a:event == 'stderr' |
| 78 | + for data in filter(a:data,'v:val !~ "^\\s*$"') |
| 79 | + echoerr data |
| 80 | + endfor |
| 81 | + endif |
| 82 | +endfunction |
| 83 | + |
| 84 | +function! s:BuildAntClasspath(hasInitTarget) |
| 85 | + let tmpBuildFile = [] |
| 86 | + for line in readfile(g:JavaComplete_AntPath) |
| 87 | + if stridx(line, '</project>') >= 0 |
| 88 | + if a:hasInitTarget |
| 89 | + let xmlTemplate = s:antXmlTemplate |
| 90 | + let xmlTemplate[0] = xmlTemplate[0][:-2]. ' depends="init">' |
| 91 | + call extend(tmpBuildFile, xmlTemplate) |
| 92 | + else |
| 93 | + call extend(tmpBuildFile, s:antXmlTemplate) |
| 94 | + endif |
| 95 | + endif |
| 96 | + call add(tmpBuildFile, line) |
| 97 | + endfor |
| 98 | + let s:tmpAntFileName = "vjc-ant-build.xml" |
| 99 | + call writefile(tmpBuildFile, s:tmpAntFileName) |
| 100 | + |
| 101 | + let s:antOutput = [] |
| 102 | + let antCmd = ['ant', '-f', s:tmpAntFileName, '-q', 'vjc-printclasspath'] |
| 103 | + call javacomplete#util#RunSystem( |
| 104 | + \ antCmd, "ant classpath build process", |
| 105 | + \ "javacomplete#classpath#ant#BuildClasspathHandler") |
| 106 | +endfunction |
| 107 | + |
| 108 | +function! javacomplete#classpath#ant#BuildClasspathHandler(data, event) |
| 109 | + if a:event == 'exit' |
| 110 | + if a:data == "0" |
| 111 | + for line in s:antOutput |
| 112 | + let matches = matchlist(line, '\m^\s\+\[echo\]\s\+\(.*\)') |
| 113 | + if !empty(matches) |
| 114 | + let cp = matches[1] |
| 115 | + break |
| 116 | + endif |
| 117 | + endfor |
| 118 | + if cp != '.' |
| 119 | + call writefile([cp], s:antPath) |
| 120 | + endif |
| 121 | + |
| 122 | + let g:JavaComplete_LibsPath .= ':'. cp |
| 123 | + |
| 124 | + call javacomplete#util#RemoveFile(javacomplete#util#GetBase('cache'). g:FILE_SEP. 'class_packages_'. g:JavaComplete_ProjectKey. '.dat') |
| 125 | + |
| 126 | + call javacomplete#server#UnblockStart() |
| 127 | + call javacomplete#server#Terminate() |
| 128 | + call javacomplete#server#Start() |
| 129 | + |
| 130 | + echomsg "Ant classpath built successfully" |
| 131 | + else |
| 132 | + echohl WarningMsg | echomsg "Failed to build ant classpath" | echohl None |
| 133 | + endif |
| 134 | + |
| 135 | + call delete(s:tmpAntFileName) |
| 136 | + |
| 137 | + unlet s:antOutput |
| 138 | + unlet s:tmpAntFileName |
| 139 | + |
| 140 | + elseif a:event == 'stdout' |
| 141 | + for data in filter(a:data,'v:val !~ "^\\s*$"') |
| 142 | + if g:JavaComplete_ShowExternalCommandsOutput |
| 143 | + echomsg data |
| 144 | + endif |
| 145 | + endfor |
| 146 | + if exists('s:antOutput') |
| 147 | + call extend(s:antOutput, a:data) |
| 148 | + endif |
| 149 | + elseif a:event == 'stderr' |
| 150 | + for data in filter(a:data,'v:val !~ "^\\s*$"') |
| 151 | + echoerr data |
| 152 | + endfor |
| 153 | + endif |
| 154 | +endfunction |
| 155 | +" vim:set fdm=marker sw=2 nowrap: |
0 commit comments