Subversion Repository Public Repository

litesoft

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
package org.litesoft.util;

import java.io.*;

import org.litesoft.commonfoundation.exceptions.*;
import org.litesoft.commonfoundation.typeutils.*;

import java.util.*;
import java.util.regex.*;

import org.litesoft.logger.*;

public class ShellUtils
{
    public static final Logger LOGGER = LoggerFactory.getLogger( ShellUtils.class );

    /**
     * Splits the specified command at spaces that are not surrounded by quotes and passes the result to {@link #shell(String...)}.
     */
    static public void shell( String command )
    {
        List<String> matchList = new ArrayList<String>();
        Pattern regex = Pattern.compile( "[^\\s\"']+|\"([^\"]*)\"|'([^']*)'" );
        Matcher regexMatcher = regex.matcher( command );
        while ( regexMatcher.find() )
        {
            if ( regexMatcher.group( 1 ) != null )
            {
                matchList.add( regexMatcher.group( 1 ) );
            }
            else if ( regexMatcher.group( 2 ) != null )
            {
                matchList.add( regexMatcher.group( 2 ) );
            }
            else
            {
                matchList.add( regexMatcher.group() );
            }
        }
        shell( matchList.toArray( new String[matchList.size()] ) );
    }

    /**
     * Executes the specified shell command. {@link #resolvePath(String)} is used to locate the file to execute. If not found, on
     * Windows the same filename with an "exe" extension is also tried.
     */
    static public void shell( String... command )
    {
        Strings.assertNotNullNotEmpty( "command", command );

        String originalCommand = (command[0] = Strings.assertNotNullNotEmpty( "shell Command", command[0] ));
        command[0] = resolvePath( command[0] );
        if ( !fileExists( command[0] ) && Utils.isWindows )
        {
            command[0] = resolvePath( command[0] + ".exe" );
            if ( !fileExists( command[0] ) )
            {
                command[0] = originalCommand;
            }
        }

        if ( LOGGER.trace.isEnabled() )
        {
            StringBuilder buffer = new StringBuilder( 256 );
            for ( String text : command )
            {
                if ( text.contains( " " ) )
                {
                    buffer.append( '"' );
                    buffer.append( text );
                    buffer.append( '"' );
                }
                else
                {
                    buffer.append( text );
                }
                buffer.append( ' ' );
            }
            LOGGER.trace.log( "Executing command: ", buffer );
        }

        try
        {
            Process process = new ProcessBuilder( command ).start();
            Thread zOut = new CopyOutputThread( process.getInputStream(), System.out );
            Thread zErr = new CopyOutputThread( process.getErrorStream(), System.err );

            zOut.join();
            zErr.join();
            // try {
            // process.waitFor();
            // } catch (InterruptedException ignored) {
            // }
            if ( process.exitValue() != 0 )
            {
                StringBuilder buffer = new StringBuilder( 256 );
                for ( String text : command )
                {
                    buffer.append( text );
                    buffer.append( ' ' );
                }
                throw new RuntimeException( "Error (" + process.exitValue() + ") executing command: " + buffer );
            }
        }
        catch ( IOException e )
        {
            throw new WrappedIOException( e );
        }
        catch ( InterruptedException e )
        {
            throw new RuntimeException( e );
        }
    }

    /**
     * Returns true if the file exists.
     */
    private static boolean fileExists( String pPath )
    {
        return new File( pPath ).isFile();
    }

    public static String resolvePath( String pPath )
    {
        String zPath = lowLevelResolve( pPath );
        LOGGER.trace.log( "Path \"", pPath, "\" resolved to: ", zPath );
        return zPath;
    }

    private static String lowLevelResolve( String fileName )
    {
        String foundFile;
        if ( fileExists( foundFile = canonical( fileName ) ) )
        {
            return foundFile;
        }
        return fileName;
    }

    /**
     * Returns the canonical path for the specified path. Eg, if "." is passed, this will resolve the actual path and return it.
     */
    private static String canonical( String path )
    {
        path = Strings.assertNotNullNotEmpty( "path", path );

        File file = new File( path );
        try
        {
            return file.getCanonicalPath();
        }
        catch ( IOException ex )
        {
            file = file.getAbsoluteFile();
            if ( file.getName().equals( "." ) )
            {
                file = file.getParentFile();
            }
            return file.getPath();
        }
    }

    public static void main( String[] args )
            throws Exception
    {
        String zEditor = System.getenv( "Editor" );
        if ( zEditor == null )
        {
            throw new IllegalStateException( "No 'Editor' environment variable" );
        }
        shell( zEditor, "src\\org\\litesoft\\util\\FileUtil.java", "src\\org\\litesoft\\util\\FileUtils.java" );
        System.out.println( "ShellUtils.main exiting..." );
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/util/ShellUtils.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

906 GeorgeS picture GeorgeS Fri 07 Jun, 2013 22:50:38 +0000

Update to latest gwt-phonegap & MGWT.