JDK-util.js revision 1641:9937077e48f1
1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * This file contains utility functions used by other tests. 26 * @subtest 27 */ 28 29var Files = Java.type('java.nio.file.Files'), 30 Paths = Java.type('java.nio.file.Paths'), 31 System = Java.type('java.lang.System') 32 33var File = java.io.File 34var windows = System.getProperty("os.name").startsWith("Windows") 35var winCyg = false 36 37var outPath = { 38 windows:0, //C:\dir 39 mixed:1 //C:/dir 40} 41 42if (windows) { 43 //Is there any better way to diffrentiate between cygwin/command prompt on windows 44 var term = System.getenv("TERM") 45 winCyg = term ? true:false 46} 47 48/** 49 * Returns which command is selected from PATH 50 * @param cmd name of the command searched from PATH 51 */ 52function which(cmd) { 53 var path = System.getenv("PATH") 54 var st = new java.util.StringTokenizer(path, File.pathSeparator) 55 while (st.hasMoreTokens()) { 56 var file = new File(st.nextToken(), cmd) 57 if (file.exists()) { 58 return (file.getAbsolutePath()) 59 } 60 } 61} 62 63/** 64 * Unix cygpath implementation 65 * Supports only two outputs,windows(C:\dir\) and mixed(C:/dir/) 66 */ 67function cygpath(path,mode) { 68 69 var newpath = path 70 if(path.startsWith("/cygdrive/")){ 71 var str = path.substring(10) 72 var index = str.indexOf('/',0) 73 var drv = str.substring(0,index) 74 var rstr = drv.toUpperCase() + ":" 75 newpath = str.replaceFirst(drv,rstr) 76 } 77 if (mode == outPath.windows) 78 return Paths.get(newpath).toString() 79 else { 80 newpath = newpath.replaceAll('\\\\','/') 81 return newpath 82 } 83 84} 85 86