src/jdk.jpackage/share/classes/jdk/jpackage/internal/ScriptRunner.java
author herrick
Wed, 23 Oct 2019 10:37:54 -0400
branchJDK-8200758-branch
changeset 58762 0fe62353385b
permissions -rw-r--r--
8232281: jpackage is not always reporting an error when no main class specified Reviewed-by: asemenyuk, asemenuk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58762
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     1
/*
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     4
 *
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    10
 *
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    15
 * accompanied this code).
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    16
 *
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    20
 *
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    23
 * questions.
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    24
 */
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    25
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    26
package jdk.jpackage.internal;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    27
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    28
import java.io.IOException;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    29
import java.nio.file.Files;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    30
import java.nio.file.Path;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    31
import java.util.Map;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    32
import java.util.Objects;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    33
import java.util.Optional;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    34
import static jdk.jpackage.internal.OverridableResource.createResource;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    35
import static jdk.jpackage.internal.StandardBundlerParam.APP_NAME;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    36
import static jdk.jpackage.internal.StandardBundlerParam.CONFIG_ROOT;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    37
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    38
/**
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    39
 * Runs custom script from resource directory.
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    40
 */
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    41
class ScriptRunner {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    42
    ScriptRunner() {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    43
        environment = new ProcessBuilder().environment();
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    44
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    45
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    46
    ScriptRunner setResourceCategoryId(String v) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    47
        resourceCategoryId = v;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    48
        return this;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    49
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    50
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    51
    ScriptRunner setDirectory(Path v) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    52
        directory = v;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    53
        return this;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    54
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    55
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    56
    ScriptRunner setScriptNameSuffix(String v) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    57
        scriptNameSuffix = v;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    58
        return this;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    59
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    60
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    61
    ScriptRunner addEnvironment(Map<String, String> v) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    62
        environment.putAll(v);
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    63
        return this;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    64
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    65
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    66
    ScriptRunner setEnvironmentVariable(String envVarName, String envVarValue) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    67
        Objects.requireNonNull(envVarName);
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    68
        if (envVarValue == null) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    69
            environment.remove(envVarName);
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    70
        } else {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    71
            environment.put(envVarName, envVarValue);
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    72
        }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    73
        return this;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    74
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    75
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    76
    public void run(Map<String, ? super Object> params) throws IOException {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    77
        String scriptName = String.format("%s-%s%s", APP_NAME.fetchFrom(params),
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    78
                scriptNameSuffix, scriptSuffix());
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    79
        Path scriptPath = CONFIG_ROOT.fetchFrom(params).toPath().resolve(
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    80
                scriptName);
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    81
        createResource(null, params)
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    82
                .setCategory(I18N.getString(resourceCategoryId))
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    83
                .saveToFile(scriptPath);
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    84
        if (!Files.exists(scriptPath)) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    85
            return;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    86
        }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    87
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    88
        ProcessBuilder pb = new ProcessBuilder(shell(),
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    89
                scriptPath.toAbsolutePath().toString());
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    90
        Map<String, String> workEnvironment = pb.environment();
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    91
        workEnvironment.clear();
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    92
        workEnvironment.putAll(environment);
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    93
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    94
        if (directory != null) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    95
            pb.directory(directory.toFile());
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    96
        }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    97
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    98
        Executor.of(pb).executeExpectSuccess();
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
    99
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   100
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   101
    private static String shell() {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   102
        if (Platform.isWindows()) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   103
            return "cscript";
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   104
        }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   105
        return Optional.ofNullable(System.getenv("SHELL")).orElseGet(() -> "sh");
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   106
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   107
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   108
    private static String scriptSuffix() {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   109
        if (Platform.isWindows()) {
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   110
            return ".wsf";
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   111
        }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   112
        return ".sh";
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   113
    }
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   114
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   115
    private String scriptNameSuffix;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   116
    private String resourceCategoryId;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   117
    private Path directory;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   118
    private Map<String, String> environment;
0fe62353385b 8232281: jpackage is not always reporting an error when no main class specified
herrick
parents:
diff changeset
   119
}