Search in sources :

Example 96 with File

use of com.codename1.io.File 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 97 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class GenerateAppProjectMojo method sourceIconFile.

private File sourceIconFile() throws IOException {
    Properties props = sourceProperties();
    String icon = props.getProperty("codename1.icon");
    if (icon == null || icon.isEmpty()) {
        icon = "icon.png";
    }
    File iconFile = new File(icon);
    if (!iconFile.isAbsolute()) {
        iconFile = new File(sourceProject, icon);
        if (iconFile.isFile()) {
            return iconFile;
        }
    }
    return new File(sourceProject, "icon.png");
}
Also used : SortedProperties(com.codename1.ant.SortedProperties)

Example 98 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class GenerateAppProjectMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        Properties props;
        try {
            props = generateAppProjectProperties();
        } catch (Exception ex) {
            throw new MojoExecutionException("Failed to load " + generateAppProjectConfigFile(), ex);
        }
        String templateType = props.getProperty("template.type");
        if (templateType == null) {
            // templateType == null is a proxy to test whether this is just a request to migrate
            // an existing ANT project.  If it is, then we will want to retain the main name and
            // package name of the original project.
            // We set the system properties here so that the call to generateProject() after this
            // will use the correct coordinates.
            Properties sourceAntProjectCn1Properties = loadSourceProjectProperties();
            if (sourceAntProjectCn1Properties.getProperty("codename1.mainName") != null) {
                System.setProperty("mainName", sourceAntProjectCn1Properties.getProperty("codename1.mainName"));
            }
            if (sourceAntProjectCn1Properties.getProperty("codename1.packageName") != null) {
                System.setProperty("packageName", sourceAntProjectCn1Properties.getProperty("codename1.packageName"));
            }
        }
        generateProject();
        if (templateType != null) {
            // This is a project template
            // Make a copy of it so that we can turn it into a concrete proejct
            File dest = new File(targetProjectDir(), path("target", "codenameone", "tmpProject"));
            dest.getParentFile().mkdirs();
            FileUtils.copyDirectory(sourceProject, dest);
            File origSource = sourceProject;
            sourceProject = dest;
            Properties renderProperties = new Properties();
            renderProperties.put("packageName", packageName());
            renderProperties.put("mainName", mainName());
            renderProperties.putAll(System.getProperties());
            ProjectTemplate tpl = new ProjectTemplate(sourceProject, renderProperties);
            if (props.getProperty("template.mainName") != null && props.getProperty("template.packageName") != null) {
                tpl.convertToTemplate(props.getProperty("template.packageName"), props.getProperty("template.mainName"));
            }
            tpl.processFiles();
        // The project should now be a concrete project
        // 
        }
        if (templateType == null || "ant".equalsIgnoreCase(templateType)) {
            // The source project was an ANT project
            copyPropertiesFiles();
            copyIcon();
            copySourceFiles();
            copyTestSourceFiles();
            copyAndroidFiles();
            copyIosFiles();
            copyJavascriptFiles();
            copyWinFiles();
            copyJavaseFiles();
            copyCSSFiles();
            copyCn1libs();
            injectDependencies();
        } else if ("maven".equalsIgnoreCase(templateType)) {
            // The source project was a Maven project
            {
                File src = new File(sourceProject, path("common", "src"));
                File destSrc = new File(targetProjectDir(), path("common", "src"));
                if (src.exists()) {
                    FileUtils.deleteDirectory(destSrc);
                    FileUtils.copyDirectory(src, destSrc);
                }
            }
            File cn1Settings = new File(sourceProject, path("common", "codenameone_settings.properties"));
            File destCn1Settings = new File(targetProjectDir(), path("common", "codenameone_settings.properties"));
            if (cn1Settings.exists()) {
                FileUtils.copyFile(cn1Settings, destCn1Settings);
            }
            for (File child : new File(sourceProject, "common").listFiles()) {
                if (child.getName().endsWith(".png")) {
                    FileUtils.copyFile(child, new File(targetProjectDir(), path("common", child.getName())));
                }
            }
            injectDependencies();
        }
    } catch (IOException ex) {
        throw new MojoExecutionException("Failed to copy files", ex);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SortedProperties(com.codename1.ant.SortedProperties) MavenExecutionException(org.apache.maven.MavenExecutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 99 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class GenerateArchetypeFromTemplateMojo method generateBaseArchetype.

private File generateBaseArchetype(String string, File templateFile) throws TemplateParseException, IOException {
    Dependency out = new Dependency();
    String archetype = extractSectionFrom(string, "archetype");
    Properties props = new Properties();
    props.load(new StringReader(archetype));
    String[] requiredProperties = new String[] { "artifactId", "groupId", "version" };
    for (String key : requiredProperties) {
        if (!props.containsKey(key)) {
            throw new TemplateParseException("archetype property " + key + " required and missing.  Make sure it is defined in the [archetype] section of the template");
        }
    }
    File dest = new File(outputDir, props.getProperty("artifactId"));
    if (dest.exists()) {
        if (overwrite) {
            FileUtils.deleteDirectory(dest);
        } else {
            throw new IOException("Project already exists at " + dest + ".  Delete this project before regenerating");
        }
    }
    String base = props.getProperty("extends", null);
    if (base == null) {
        throw new TemplateParseException("[archetype] section requires the 'extends' property to specify the path to the archetype project that this extends");
    }
    baseArchetypeDir = new File(base);
    if (!baseArchetypeDir.isAbsolute()) {
        baseArchetypeDir = new File(templateFile.getParentFile(), base);
    }
    if (!baseArchetypeDir.exists()) {
        throw new IOException("Cannot find archetype project that this template extends.  Looking for it in " + baseArchetypeDir);
    }
    if (!new File(baseArchetypeDir, "pom.xml").exists()) {
        throw new IOException("Base archetype directory " + baseArchetypeDir + " is not a maven project.");
    }
    FileUtils.copyDirectory(baseArchetypeDir, dest);
    File pomFile = new File(dest, "pom.xml");
    String groupId = null;
    String artifactId = null;
    String version = null;
    if (props.containsKey("id")) {
        String id = props.getProperty("id");
        String[] parts = id.split(":");
        if (parts.length != 3) {
            throw new TemplateParseException("Failed ot parse id property in [archetype] section.  It should be in the format groupId:artifactId:version");
        }
        groupId = parts[0];
        artifactId = parts[1];
        version = parts[2];
    }
    groupId = props.getProperty("groupId", groupId);
    artifactId = props.getProperty("artifactId", artifactId);
    version = props.getProperty("version", version);
    if (groupId == null || artifactId == null || version == null) {
        throw new TemplateParseException("The [archetype] section is required, and must have at least groupId, artifactId, and version defined.  You may also define these using the id property in the format groupId:artifactId:version");
    }
    String parentTag = "";
    String parentGroupId = null;
    String parentArtifactId = null;
    String parentVersion = null;
    if (props.containsKey("parent")) {
        String parent = props.getProperty("parent");
        String[] parts = parent.split(":");
        if (parts.length != 3) {
            throw new TemplateParseException("Failed to parse parent property in [archetype] section. It should be in the format groupId:artifactId:version");
        }
        parentGroupId = parts[0];
        parentArtifactId = parts[1];
        parentVersion = parts[2];
    }
    parentGroupId = props.getProperty("parentGroupId", parentGroupId);
    parentArtifactId = props.getProperty("parentArtifactId", parentArtifactId);
    parentVersion = props.getProperty("parentVersion", parentVersion);
    if (parentGroupId != null && parentVersion != null && parentArtifactId != null) {
        parentTag = "  <parent>\n" + "    <groupId>" + parentGroupId + "</groupId>\n" + "    <artifactId>" + parentArtifactId + "</artifactId>\n" + "    <version>" + parentVersion + "</version>\n" + "  </parent>\n";
    }
    String pomContents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + "  <modelVersion>4.0.0</modelVersion>\n" + parentTag + "  <groupId>" + groupId + "</groupId>\n" + "  <artifactId>" + artifactId + "</artifactId>\n" + "  <version>" + version + "</version>\n" + "  <packaging>maven-archetype</packaging>\n" + "\n" + "  <name>" + artifactId + "</name>\n" + "\n" + "\n" + "  <build>\n" + "    <extensions>\n" + "      <extension>\n" + "        <groupId>org.apache.maven.archetype</groupId>\n" + "        <artifactId>archetype-packaging</artifactId>\n" + "        <version>3.2.0</version>\n" + "      </extension>\n" + "    </extensions>\n" + "\n" + "    <pluginManagement>\n" + "      <plugins>\n" + "        <plugin>\n" + "          <artifactId>maven-archetype-plugin</artifactId>\n" + "          <version>3.2.0</version>\n" + "        </plugin>\n" + "      </plugins>\n" + "    </pluginManagement>\n" + "  </build>\n" + "\n" + "  <description>Artifact generated using the cn1:generate-archetype goal</description>\n" + "\n" + "  <url>https://www.codenameone.com</url>\n" + "\n" + "  <licenses>\n" + "    <license>\n" + "      <name>GPL v2 With Classpath Exception</name>\n" + "      <url>https://openjdk.java.net/legal/gplv2+ce.html</url>\n" + "      <distribution>repo</distribution>\n" + "      <comments>A business-friendly OSS license</comments>\n" + "    </license>\n" + "  </licenses>\n" + "</project>\n";
    FileUtils.writeStringToFile(pomFile, pomContents, "UTF-8");
    return dest;
}
Also used : Dependency(org.apache.maven.model.Dependency) SortedProperties(com.codename1.ant.SortedProperties)

Example 100 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class GenerateGuiSourcesMojo method executeImpl.

@Override
protected void executeImpl() throws MojoExecutionException, MojoFailureException {
    if (!isCN1ProjectDir()) {
        return;
    }
    if (System.getProperty("generate-gui-sources-done") != null) {
        return;
    }
    System.setProperty("generate-gui-sources-done", "true");
    System.setProperty("javax.xml.bind.context.factory", "com.sun.xml.bind.v2.ContextFactory");
    GenerateGuiSources g = new GenerateGuiSources();
    g.setSrcDir(new File(getCN1ProjectDir(), "src" + File.separator + "main" + File.separator + "java"));
    g.setGuiDir(new File(getCN1ProjectDir(), "src" + File.separator + "main" + File.separator + "guibuilder"));
    g.execute();
    // Generate the RAD templates while we're at it
    File radViews = getRADViewsDirectory();
    getLog().debug("Looking for views in " + radViews);
    if (radViews.isDirectory()) {
        project.addCompileSourceRoot(getRADGeneratedSourcesDirectory().getAbsolutePath());
        Exception res = forEach(radViews, child -> {
            if (!child.getName().endsWith(".xml")) {
                return null;
            }
            File destClassFile = getDestClassForRADView(child);
            getLog().debug("Found view " + child + ".  Checking against " + destClassFile);
            if (!destClassFile.exists() || child.lastModified() > destClassFile.lastModified()) {
                try {
                    generateRADViewClass(child);
                } catch (IOException ex) {
                    return new MojoFailureException("Failed to generate class for RAD fragment XML file " + child, ex);
                }
            }
            return null;
        });
        if (res != null) {
            if (res instanceof MojoExecutionException) {
                throw (MojoFailureException) res;
            } else {
                throw new MojoFailureException("Failed to compile RAD views:" + res.getMessage(), res);
            }
        }
    }
}
Also used : GenerateGuiSources(com.codename1.build.client.GenerateGuiSources) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Aggregations

IOException (java.io.IOException)76 File (java.io.File)61 FileInputStream (java.io.FileInputStream)43 InputStream (java.io.InputStream)33 EncodedImage (com.codename1.ui.EncodedImage)23 FileOutputStream (java.io.FileOutputStream)23 OutputStream (java.io.OutputStream)22 SortedProperties (com.codename1.ant.SortedProperties)18 FileSystemStorage (com.codename1.io.FileSystemStorage)17 BufferedInputStream (com.codename1.io.BufferedInputStream)15 Image (com.codename1.ui.Image)15 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ActionEvent (com.codename1.ui.events.ActionEvent)14 RandomAccessFile (java.io.RandomAccessFile)14 ArrayList (java.util.ArrayList)14 EditableResources (com.codename1.ui.util.EditableResources)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 Properties (java.util.Properties)12 File (com.codename1.io.File)11 BufferedImage (java.awt.image.BufferedImage)11