Search in sources :

Example 6 with SortedProperties

use of com.codename1.ant.SortedProperties in project CodenameOne by codenameone.

the class InstallCn1libsMojo method getLibraryRequiredProperties.

/**
 * Required properties for library.
 * @param artifact
 * @return
 * @throws IOException
 */
private SortedProperties getLibraryRequiredProperties(Artifact artifact) throws IOException {
    SortedProperties out = new SortedProperties();
    File file = getLibraryRequiredPropertiesFile(artifact);
    if (file.exists()) {
        try (FileInputStream fis = new FileInputStream(file)) {
            out.load(fis);
        }
    }
    return out;
}
Also used : SortedProperties(com.codename1.ant.SortedProperties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with SortedProperties

use of com.codename1.ant.SortedProperties in project CodenameOne by codenameone.

the class ProjectTemplate method processFiles.

public void processFiles() throws IOException {
    process(projectRoot);
    File cn1SettingsFile = new File(projectRoot, "codenameone_settings.properties");
    if (!cn1SettingsFile.exists()) {
        cn1SettingsFile = new File(projectRoot, path("common", "codenameone_settings.properties"));
    }
    if (cn1SettingsFile.exists()) {
        SortedProperties props = new SortedProperties();
        try (FileInputStream fis = new FileInputStream(cn1SettingsFile)) {
            props.load(fis);
        }
        props.setProperty("codename1.packageName", properties.getProperty("packageName"));
        props.setProperty("codename1.mainName", properties.getProperty("mainName"));
        try (FileOutputStream fos = new FileOutputStream(cn1SettingsFile)) {
            props.store(fos, "Updated mainName and packageName");
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SortedProperties(com.codename1.ant.SortedProperties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with SortedProperties

use of com.codename1.ant.SortedProperties in project CodenameOne by codenameone.

the class CN1BuildMojo method mergeRequiredProperties.

private SortedProperties mergeRequiredProperties(String libraryName, Properties libProps, Properties projectProps) throws LibraryPropertiesException {
    String javaVersion = (String) projectProps.getProperty("codename1.arg.java.version", "8");
    String javaVersionLib = (String) libProps.get("codename1.arg.java.version");
    if (javaVersionLib != null) {
        int v1 = 5;
        if (javaVersion != null) {
            v1 = Integer.parseInt(javaVersion);
        }
        int v2 = Integer.parseInt(javaVersionLib);
        // if the lib java version is bigger, this library cannot be used
        if (v1 < v2) {
            throw new VersionMismatchException(libraryName, "Cannot use a cn1lib with java version " + "greater then the project java version");
        }
    }
    // merge and save
    SortedProperties merged = new SortedProperties();
    merged.putAll(projectProps);
    Enumeration keys = libProps.propertyNames();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        if (!merged.containsKey(key)) {
            merged.put(key, libProps.getProperty(key));
        } else {
            // install will fail
            if (!merged.get(key).equals(libProps.getProperty(key))) {
                throw new PropertyConflictException(libraryName, "Property " + key + " has a conflict");
            }
        }
    }
    return merged;
}
Also used : SortedProperties(com.codename1.ant.SortedProperties)

Example 9 with SortedProperties

use of com.codename1.ant.SortedProperties in project CodenameOne by codenameone.

the class GenerateAppProjectMojo method copyIcon.

private void copyIcon() throws IOException {
    File sourceIconFile = sourceIconFile();
    if (sourceIconFile.exists() && sourceIconFile.isFile()) {
        FileUtils.copyFile(sourceIconFile(), destIconFile());
    } else {
        try (InputStream iconStream = getClass().getResourceAsStream("codenameone-icon.png")) {
            FileUtils.copyInputStreamToFile(iconStream, destIconFile());
        }
    }
    SortedProperties props = new SortedProperties();
    File propertiesFile = new File(targetCommonDir(), "codenameone_settings.properties");
    try (InputStream input = new FileInputStream(propertiesFile)) {
        props.load(input);
    }
    if (!destIconFile().getName().equals(props.getProperty("codename1.icon"))) {
        props.setProperty("codename1.icon", destIconFile().getName());
        try (OutputStream output = new FileOutputStream(propertiesFile)) {
            props.store(output, "Updated icon");
        }
    }
}
Also used : SortedProperties(com.codename1.ant.SortedProperties)

Example 10 with SortedProperties

use of com.codename1.ant.SortedProperties in project CodenameOne by codenameone.

the class ProjectTemplate method convertToTemplate.

/**
 * Convert a real project to a template.
 * This mainly consists changing all references to the specified packageName and mainName
 * to be changed the the ${packageName} and ${mainName} variables.
 *
 * @param packageName
 * @param mainName
 */
public void convertToTemplate(String packageName, String mainName) throws IOException {
    File codenameOneSettings = new File(projectRoot, "codenameone_settings.properties");
    SortedProperties settingsProps = new SortedProperties();
    String path;
    if (codenameOneSettings.exists()) {
        // This is an ant project
        try (FileInputStream fis = new FileInputStream(codenameOneSettings)) {
            settingsProps.load(fis);
        }
        settingsProps.put("codename1.packageName", "${packageName}");
        settingsProps.put("codename1.mainName", "${mainName}");
        try (FileOutputStream fos = new FileOutputStream(codenameOneSettings)) {
            settingsProps.store(fos, "Updated packageName and mainName");
        }
        File srcDir = new File(projectRoot, "src");
        if (srcDir.exists()) {
            convertToTemplate(packageName, mainName, srcDir);
        }
        path = packageName.replace('.', File.separatorChar);
        File packageDirectory = new File(srcDir, path);
        if (packageDirectory.exists()) {
            File dest = new File(srcDir, "__packagePath__");
            FileUtils.moveDirectory(packageDirectory, dest);
            for (File child : dest.listFiles()) {
                if (child.getName().equals(mainName + ".java") || child.getName().equals(mainName + ".kt") || child.getName().equals(mainName + ".mirah")) {
                    File destMain = new File(dest, "__mainName__" + child.getName().substring(child.getName().lastIndexOf(".")));
                    FileUtils.moveFile(child, destMain);
                }
            }
            convertToTemplate(packageName, mainName, dest);
        }
        return;
    }
    codenameOneSettings = new File(projectRoot, path("common", "codenameone_settings.properties"));
    if (codenameOneSettings.exists()) {
        // This is a maven project.
        try (FileInputStream fis = new FileInputStream(codenameOneSettings)) {
            settingsProps.load(fis);
        }
        settingsProps.put("codename1.packageName", "${packageName}");
        settingsProps.put("codename1.mainName", "${mainName}");
        try (FileOutputStream fos = new FileOutputStream(codenameOneSettings)) {
            settingsProps.store(fos, "Updated packageName and mainName");
        }
        for (String lang : new String[] { "java", "kotlin", "mirah", "resources", "rad" + File.separator + "views" }) {
            File srcDir = new File(codenameOneSettings.getParentFile(), path("src", "main", lang));
            if (srcDir.exists()) {
                convertToTemplate(packageName, mainName, srcDir);
            }
            path = packageName.replace('.', File.separatorChar);
            File packageDirectory = new File(srcDir, path);
            if (packageDirectory.exists()) {
                File dest = new File(srcDir, "__packagePath__");
                FileUtils.moveDirectory(packageDirectory, dest);
                for (File child : dest.listFiles()) {
                    if (child.getName().equals(mainName + ".java") || child.getName().equals(mainName + ".kt") || child.getName().equals(mainName + ".mirah")) {
                        File destMain = new File(dest, "__mainName__" + child.getName().substring(child.getName().lastIndexOf(".")));
                        FileUtils.moveFile(child, destMain);
                    }
                }
                convertToTemplate(packageName, mainName, dest);
            }
        }
        return;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SortedProperties(com.codename1.ant.SortedProperties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

SortedProperties (com.codename1.ant.SortedProperties)12 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 FileOutputStream (java.io.FileOutputStream)2 FileObject (org.apache.commons.vfs2.FileObject)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 BufferedImage (java.awt.image.BufferedImage)1 Enumeration (java.util.Enumeration)1 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1 PatternFileSelector (org.apache.commons.vfs2.PatternFileSelector)1 Artifact (org.apache.maven.artifact.Artifact)1 BuildException (org.apache.tools.ant.BuildException)1 Copy (org.apache.tools.ant.taskdefs.Copy)1 Expand (org.apache.tools.ant.taskdefs.Expand)1 FileSet (org.apache.tools.ant.types.FileSet)1