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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package com.esotericsoftware.scar;

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

import com.esotericsoftware.filesystem.*;
import com.esotericsoftware.scar.support.*;
import com.esotericsoftware.utils.*;

import static com.esotericsoftware.scar.support.Parameter.*;

@SuppressWarnings({"UnusedDeclaration"})
public class ProjectParameters extends FileUtil
{
    public static final Parameter NAME = def( "name", Form.STRING, "The name of the project. Used to name the JAR.", //
                                              "Default:\n" + //
                                              "  The name of the directory containing the project YAML file, or\n" + //
                                              "  the name of the YAML file if it is not 'build'." );

    public static final Parameter VERSION = def( "version", Form.STRING, "The version of the project. If available, used to name the 'default' named JAR." );

    public static final Parameter MAIN = def( "main", Form.STRING, "Name of the main class." );

    public static final Parameter TARGET = def( "target", Form.STRING, "The directory to output build artifacts.", //
                                                "Default: The directory containing the project YAML file, plus 'build'." );

    public static final Parameter DEPENDENCIES = def( "dependencies", Form.STRING_LIST, "Relative or absolute path(s) to dependency project directories or YAML files." );

    public static final Parameter CLASSPATH = def( "classpath", Form.PATHS, "Wildcard patterns for the file(s) to include on the classpath.", //
                                                   "Default: 'lib|**.jar'." );

    public static final Parameter SOURCE = def( "source", Form.PATHS, "Wildcard patterns for the Java file(s) to compile.", //
                                                "Default: 'src|**.java' or 'src/main/java|**.java'." );

    public static final Parameter RESOURCES = def( "resources", Form.PATHS, "Wildcard patterns for the file(s) to include in the JAR.", //
                                                   "Default: 'resources' or 'src/main/resources'." );

    public static final Parameter JAR = def( "jar", Form.STRING, "JAR name w/ optional path for the JAR ('.jar' added to the end if does not end with 'jar', case insensitive).", //
                                             "Default: '$target$[-$version$]'." );

    public static final Parameter DIST = def( "dist", Form.PATHS, "Wildcard patterns for the file(s) to include in the distribution, outside the JAR." );

    public static final Parameter APPDIR = def( "appdir", Form.STRING, "Directory path to bring together all files (both JARs and 'dist', for this project " + "and all dependencies, recursively) the application needs to be run from JAR files." );

    // ------------------------------------------------ Default Support ------------------------------------------------

    protected synchronized void applyDefaults()
    {
        defaultTARGET();
        defaultCLASSPATH();
        defaultSOURCE();
        defaultRESOURCES();
        defaultJAR();
    }

    protected void defaultJAR()
    {
        String zJar = getJar();
        if ( null == zJar )
        {
            mManager.put( JAR.getName(), "$target$/$name$" + (hasVersion() ? "-$version$" : "") + ".jar" );
        }
        else
        {
            int at = zJar.lastIndexOf( '.' );
            if ( at == -1 || !".jar".equalsIgnoreCase( zJar.substring( at ) ) )
            {
                mManager.put( JAR.getName(), zJar + ".jar" );
            }
        }
    }

    protected void defaultTARGET()
    {
        defaultKey( TARGET, "build" );
    }

    private void defaultCLASSPATH()
    {
        defaultSubDirOptional( CLASSPATH, "lib|**.jar" );
    }

    private void defaultRESOURCES()
    {
        defaultSubDirOptional( RESOURCES, "src/main/resources", "resources" );
    }

    private void defaultSOURCE()
    {
        defaultSubDirOptional( SOURCE, "src/main/java|**.java", "src|**.java" );
    }

    // ------------------------------------------- Special Property Accessors ------------------------------------------

    public boolean hasVersion()
    {
        return (null != getVersion());
    }

    public String getVersion()
    {
        return get( JAR.getName() );
    }

    public boolean hasMain()
    {
        return (null != getMain());
    }

    public String getMain()
    {
        return get( MAIN.getName() );
    }

    public String getTarget()
    {
        return get( TARGET.getName() );
    }

    public List<String> getDependencies()
    {
        return getListNotNull( DEPENDENCIES.getName() );
    }

    public Paths getClasspath()
    {
        return getPaths( CLASSPATH.getName() );
    }

    public Paths getSource()
    {
        return getPaths( SOURCE.getName() );
    }

    public Paths getResources()
    {
        return getPaths( RESOURCES.getName() );
    }

    public String getJar()
    {
        return get( JAR.getName() );
    }

    public Paths getDist()
    {
        return getPaths( DIST.getName() );
    }

    public String getAppDir()
    {
        return get( APPDIR.getName() );
    }

    // ---------------------------------- Generic accessors for the underlying Data (map) ------------------------------

    public boolean has( Object key )
    {
        return null != getObject( key );
    }

    public Object getObject( Object key )
    {
        return mManager.get( mManager.normalizeKey( key ) );
    }

    public Object getObject( Object key, Object defaultValue )
    {
        return Util.deNull( getObject( key ), defaultValue );
    }

    public String get( Object key )
    {
        Object zValue = getObject( key );
        return (zValue != null) ? zValue.toString() : null;
    }

    public String get( Object key, String defaultValue )
    {
        return Util.deNull( get( key ), defaultValue );
    }

    public int get_int( Object key )
    {
        return get_int( key, 0 );
    }

    public int get_int( Object key, int defaultValue )
    {
        return getInteger( key, defaultValue );
    }

    public Integer getInteger( Object key, int defaultValue )
    {
        return Util.deNull( getInteger( key ), defaultValue );
    }

    public Integer getInteger( Object key )
    {
        return getCachedWithConvertion( key, INTEGER );
    }

    public float get_float( Object key )
    {
        return get_float( key, 0 );
    }

    public float get_float( Object key, float defaultValue )
    {
        return getFloat( key, defaultValue );
    }

    public Float getFloat( Object key, float defaultValue )
    {
        return Util.deNull( getFloat( key ), defaultValue );
    }

    public Float getFloat( Object key )
    {
        return getCachedWithConvertion( key, FLOAT );
    }

    public boolean get_boolean( Object key )
    {
        return get_boolean( key, false );
    }

    public boolean get_boolean( Object key, boolean defaultValue )
    {
        return getBoolean( key, defaultValue );
    }

    public Boolean getBoolean( Object key, boolean defaultValue )
    {
        return Util.deNull( getBoolean( key ), defaultValue );
    }

    public Boolean getBoolean( Object key )
    {
        return getCachedWithConvertion( key, BOOLEAN );
    }

    /**
     * Returns a list of objects under the specified key. If the key is a single value, it is placed in a list and returned. If the
     * key does not exist, a list with the defaultValues is returned.
     */
    public List<?> getObjectListNotNull( Object key, Object... defaultValues )
    {
        List<?> zList = getObjectList( key );
        return (zList != null) ? zList : (defaultValues == null) ? Collections.emptyList() : Arrays.asList( defaultValues );
    }

    /**
     * Returns a list of objects under the specified key. If the key is a single value, it is placed in a list and returned. If the
     * key does not exist, a 'null' is returned.
     */
    public List<?> getObjectList( Object key )
    {
        return getCachedWithConvertion( key, LIST_OBJECT );
    }

    /**
     * Returns a list of strings under the specified key. If the key is a single value, it is placed in a list and returned. If the
     * key does not exist, a list with the defaultValues is returned.
     */
    public List<String> getListNotNull( Object key, String... defaultValues )
    {
        List<String> zList = getList( key );
        return (zList != null) ? zList : (defaultValues == null) ? Collections.<String>emptyList() : Arrays.asList( defaultValues );
    }

    /**
     * Returns a list of strings under the specified key. If the key is a single value, it is placed in a list and returned. If the
     * key does not exist, a 'null' is returned.
     */
    public List<String> getList( Object key )
    {
        return getCachedWithConvertion( key, LIST_STRING );
    }

    /**
     * Returns a Map<Object,Object> under the specified key. If the key does not exist, a Map with the defaultValues (as Key/Value pairs) is returned.
     */
    public Map<?, ?> getObjectMapNotNull( Object key, Object... defaultValues )
    {
        Util.assertPairedEntries( "defaultValues", defaultValues );
        Map<?, ?> map = getObjectMap( key );
        return (map != null) ? map : createMap( defaultValues );
    }

    /**
     * Returns a Map<Object,Object> under the specified key. If the key does not exist, a 'null' is returned.
     */
    public Map<?, ?> getObjectMap( Object key )
    {
        return getCachedWithConvertion( key, MAP_OBJECT );
    }

    /**
     * Returns a Map<String,String> under the specified key. If the key does not exist, a Map with the defaultValues (as Key/Value pairs) is returned.
     */
    public Map<String, String> getMapNotNull( Object key, String... defaultValues )
    {
        Util.assertPairedEntries( "defaultValues", defaultValues );
        Map<String, String> map = getMap( key );
        return (map != null) ? map : createMap( defaultValues );
    }

    /**
     * Returns a Map<String, String> under the specified key. If the key does not exist, a 'null' is returned.
     */
    public Map<String, String> getMap( Object key )
    {
        return getCachedWithConvertion( key, MAP_STRING );
    }

    /**
     * Uses the strings under the specified key to {@link Paths#glob(String, String...) glob} paths.
     */
    public Paths getPaths( String key )
    {
        Paths zPaths = getCachedWithConvertion( key, PATHS );
        return (zPaths != null) ? zPaths : new Paths();
    }

    /**
     * Returns a canonicalizePath built from the specified path.
     * If the specified path is a relative path, it is made absolute relative to this project's directory.
     */
    public String path( String path )
    {
        if ( path == null )
        {
            return null;
        }
        path = format( path );
        String zSuffix = "";
        int pipeIndex = path.indexOf( '|' );
        if ( pipeIndex > -1 )
        {
            // Handle wildcard search patterns.
            zSuffix = path.substring( pipeIndex );
            path = path.substring( 0, pipeIndex );
        }
        return canonicalizePath( getCanonicalProjectDir(), path ).getPath() + zSuffix;
    }

    /**
     * Replaces property names surrounded by dollar-signs ('$') with the value from this project.
     */
    public String format( String text )
    {
        Matcher matcher = formatPattern.matcher( text );
        StringBuilder buffer = new StringBuilder( 128 );
        while ( matcher.find() )
        {
            buffer.append( matcher.group( 1 ) );
            String name = matcher.group( 2 );
            Object value = getObject( name );
            if ( value instanceof String )
            {
                buffer.append( format( (String) value ) );
            }
            else if ( value != null )
            {
                buffer.append( value );
            }
            else
            {
                buffer.append( name );
            }
            buffer.append( matcher.group( 3 ) );
        }
        if ( buffer.length() == 0 )
        {
            return text;
        }
        return buffer.toString();
    }

    public Object[] keys()
    {
        return mManager.keys();
    }

    public File getCanonicalProjectDir()
    {
        return mCanonicalProjectDir;
    }

    public String getName()
    {
        return mName;
    }

    public ProjectParameters( String pName, File pCanonicalProjectDir, Map<Object, Object> pData )
    {
        this( pName, pCanonicalProjectDir, new Manager( pData ) );
    }

    // ---------------------------------------------------- Support ----------------------------------------------------

    protected ProjectParameters( ProjectParameters pParameters )
    {
        this( pParameters.getName(), pParameters.getCanonicalProjectDir(), pParameters.mManager );
    }

    private ProjectParameters( String pName, File pCanonicalProjectDir, Manager pManager )
    {
        mCanonicalProjectDir = pCanonicalProjectDir;
        mManager = pManager;
        Object zName = mManager.get( NAME.getName() );
        mManager.put( NAME.getName(), mName = (zName != null) ? zName.toString() : pName );
    }

    protected final String mName;
    protected final File mCanonicalProjectDir;
    protected final Manager mManager;

    static private final java.util.regex.Pattern formatPattern = java.util.regex.Pattern.compile( "([^\\$]*)\\$([^\\$]+)\\$([^\\$]*)" );

    @SuppressWarnings({"unchecked"})
    private <T> Map<T, T> getAsMap( Object pKey )
    {
        Object zValue = getObject( pKey );
        return (zValue instanceof Map) ? (Map<T, T>) zValue : null;
    }

    @Override
    public String toString()
    {
        return getName();
    }

    protected void deletePath( String pProjectRelativePath )
    {
        String zPath = path( pProjectRelativePath );
        if ( zPath != null )
        {
            delete( zPath );
        }
    }

    protected void defaultSubDirOptional( Parameter pParameter, String... pOptions )
    {
        Object o = mManager.get( pParameter.getName() );
        if ( o == null )
        {
            for ( String zOption : pOptions )
            {
                if ( dirExists( zOption ) )
                {
                    mManager.put( pParameter.getName(), zOption );
                    return;
                }
            }
        }
    }

    protected boolean dirExists( String pPath )
    {
        int pipeAt = pPath.indexOf( '|' );
        if ( pipeAt != -1 )
        {
            pPath = pPath.substring( pipeAt );
        }
        return new File( getCanonicalProjectDir(), pPath ).isDirectory();
    }

    protected void defaultKey( Parameter pParameter, String pDefault )
    {
        if ( null == get( pParameter.getName() ) )
        {
            mManager.put( pParameter.getName(), pDefault );
        }
    }

    protected <T> Map<T, T> createMap( T[] defaultValues )
    {
        Map<T, T> map = new HashMap<T, T>();
        if ( defaultValues != null )
        {
            for ( int i = 0; i < defaultValues.length; )
            {
                T defaultKey = defaultValues[i++];
                T defaultValue = defaultValues[i++];
                map.put( defaultKey, defaultValue );
            }
        }
        return map;
    }

    protected <T> T getCachedWithConvertion( Object pKey, DataConverter<T> pConverter )
    {
        Object zValue = mManager.getCachedResponse( pKey = mManager.normalizeKey( pKey ) );
        if ( zValue != null )
        {
            //noinspection unchecked
            return (T) zValue;
        }
        zValue = mManager.get( pKey );
        if ( zValue == null )
        {
            return null;
        }
        T zConverted = pConverter.convert( zValue );
        mManager.addCachedResponse( pKey, zConverted );
        return zConverted;
    }

    private static final DataConverter<Integer> INTEGER = new DataConverter<Integer>()
    {
        @Override
        public Integer convert( Object pValue )
        {
            return (pValue instanceof Number) ? ((Number) pValue).intValue() : Integer.parseInt( pValue.toString() );
        }
    };

    private static final DataConverter<Float> FLOAT = new DataConverter<Float>()
    {
        @Override
        public Float convert( Object pValue )
        {
            return (pValue instanceof Number) ? ((Number) pValue).floatValue() : Float.parseFloat( pValue.toString() );
        }
    };

    private static final DataConverter<Boolean> BOOLEAN = new DataConverter<Boolean>()
    {
        @Override
        public Boolean convert( Object pValue )
        {
            return (pValue instanceof Boolean) ? (Boolean) pValue : Boolean.parseBoolean( pValue.toString() );
        }
    };

    private static final DataConverter<List<?>> LIST_OBJECT = new DataConverter<List<?>>()
    {
        @Override
        public List<?> convert( Object pValue )
        {
            return (pValue instanceof List) ? (List<?>) pValue : Arrays.asList( pValue );
        }
    };

    private static final DataConverter<List<String>> LIST_STRING = new DataConverter<List<String>>()
    {
        @Override
        public List<String> convert( Object pValue )
        {
            //noinspection unchecked
            return (pValue instanceof List) ? (List<String>) pValue : Arrays.asList( pValue.toString() );
        }
    };

    private static final DataConverter<Map<?, ?>> MAP_OBJECT = new DataConverter<Map<?, ?>>()
    {
        @Override
        public Map<?, ?> convert( Object pValue )
        {
            return (Map<?, ?>) pValue;
        }
    };

    private static final DataConverter<Map<String, String>> MAP_STRING = new DataConverter<Map<String, String>>()
    {
        @Override
        public Map<String, String> convert( Object pValue )
        {
            //noinspection unchecked
            return (Map<String, String>) pValue;
        }
    };

    private final DataConverter<String> PATH = new DataConverter<String>()
    {
        @Override
        public String convert( Object pValue )
        {
            return path( pValue.toString() );
        }
    };

    private final DataConverter<Paths> PATHS = new DataConverter<Paths>()
    {
        @Override
        public Paths convert( Object pValue )
        {
            Paths paths = new Paths();
            List<String> zList = LIST_STRING.convert( pValue );
            for ( String dirPattern : zList )
            {
                paths.glob( path( dirPattern ) );
            }
            return paths;
        }
    };
}

Commits for litesoft/trunk/Java/ScarPlus/src/com/esotericsoftware/scar/ProjectParameters.java

Diff revisions: vs.
Revision Author Commited Message
316 Diff Diff GeorgeS picture GeorgeS Tue 19 Jul, 2011 01:02:37 +0000
315 Diff Diff GeorgeS picture GeorgeS Sun 17 Jul, 2011 15:48:36 +0000
308 Diff Diff GeorgeS picture GeorgeS Sun 10 Jul, 2011 23:55:06 +0000
302 Diff Diff GeorgeS picture GeorgeS Fri 08 Jul, 2011 14:08:10 +0000
299 Diff Diff GeorgeS picture GeorgeS Tue 05 Jul, 2011 04:45:50 +0000
292 Diff Diff GeorgeS picture GeorgeS Wed 22 Jun, 2011 16:35:51 +0000
291 Diff Diff GeorgeS picture GeorgeS Wed 22 Jun, 2011 00:33:37 +0000
290 Diff Diff GeorgeS picture GeorgeS Tue 21 Jun, 2011 14:58:27 +0000
289 GeorgeS picture GeorgeS Tue 21 Jun, 2011 00:43:29 +0000