ada.project/src/org/netbeans/modules/ada/project/ui/actions/BuildCommand.java
author Andrea Lucarelli <raster@netbeans.org>
Sun, 22 Aug 2010 23:37:11 +0200
branchrelease68
changeset 16367 d2820c029d3a
parent 15779 367c7fdb5d23
permissions -rw-r--r--
Add JVM compiler support.
raster@14352
     1
/*
raster@14352
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
raster@14352
     3
 *
raster@14352
     4
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
raster@14352
     5
 *
raster@14352
     6
 * The contents of this file are subject to the terms of either the GNU
raster@14352
     7
 * General Public License Version 2 only ("GPL") or the Common
raster@14352
     8
 * Development and Distribution License("CDDL") (collectively, the
raster@14352
     9
 * "License"). You may not use this file except in compliance with the
raster@14352
    10
 * License. You can obtain a copy of the License at
raster@14352
    11
 * http://www.netbeans.org/cddl-gplv2.html
raster@14352
    12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
raster@14352
    13
 * specific language governing permissions and limitations under the
raster@14352
    14
 * License.  When distributing the software, include this License Header
raster@14352
    15
 * Notice in each file and include the License file at
raster@14352
    16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
raster@14352
    17
 * particular file as subject to the "Classpath" exception as provided
raster@14352
    18
 * by Sun in the GPL Version 2 section of the License file that
raster@14352
    19
 * accompanied this code. If applicable, add the following below the
raster@14352
    20
 * License Header, with the fields enclosed by brackets [] replaced by
raster@14352
    21
 * your own identifying information:
raster@14352
    22
 * "Portions Copyrighted [year] [name of copyright owner]"
raster@14352
    23
 *
raster@14352
    24
 * If you wish your version of this file to be governed by only the CDDL
raster@14352
    25
 * or only the GPL Version 2, indicate your decision by adding
raster@14352
    26
 * "[Contributor] elects to include this software in this distribution
raster@14352
    27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
raster@14352
    28
 * single choice of license, a recipient has the option to distribute
raster@14352
    29
 * your version of this file under either the CDDL, the GPL Version 2 or
raster@14352
    30
 * to extend the choice of license to its licensees as provided above.
raster@14352
    31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
raster@14352
    32
 * Version 2 license, then the option applies only if the new code is
raster@14352
    33
 * made subject to such option by the copyright holder.
raster@14352
    34
 *
raster@14352
    35
 * Contributor(s):
raster@14352
    36
 *
raster@14352
    37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
raster@14352
    38
 */
raster@14352
    39
package org.netbeans.modules.ada.project.ui.actions;
raster@14352
    40
raster@14461
    41
import java.io.File;
raster@14461
    42
import java.io.IOException;
raster@15779
    43
import java.util.ArrayList;
raster@14443
    44
import org.netbeans.api.ada.platform.AdaPlatform;
raster@14698
    45
import org.netbeans.modules.ada.platform.compiler.gnat.GnatCompiler;
raster@14352
    46
import org.netbeans.modules.ada.project.AdaActionProvider;
raster@14352
    47
import org.netbeans.modules.ada.project.AdaProject;
raster@14443
    48
import org.netbeans.modules.ada.project.AdaProjectUtil;
raster@15779
    49
import org.netbeans.modules.ada.project.options.AdaOptions;
raster@14443
    50
import org.netbeans.modules.ada.project.ui.properties.AdaProjectProperties;
raster@14461
    51
import org.openide.filesystems.FileObject;
raster@14461
    52
import org.openide.filesystems.FileUtil;
raster@14461
    53
import org.openide.util.Exceptions;
raster@14352
    54
import org.openide.util.Lookup;
raster@14352
    55
raster@14352
    56
/**
raster@14352
    57
 *
raster@14352
    58
 * @author Andrea Lucarelli
raster@14352
    59
 */
raster@14352
    60
public class BuildCommand extends Command {
raster@14352
    61
raster@14352
    62
    private static final String COMMAND_ID = AdaActionProvider.COMMAND_BUILD;
raster@14352
    63
raster@14352
    64
    public BuildCommand(AdaProject project) {
raster@14352
    65
        super(project);
raster@14352
    66
    }
raster@14352
    67
raster@14352
    68
    @Override
raster@14352
    69
    public String getCommandId() {
raster@14352
    70
        return COMMAND_ID;
raster@14352
    71
    }
raster@14352
    72
raster@14352
    73
    @Override
raster@14352
    74
    public void invokeAction(Lookup context) throws IllegalArgumentException {
raster@14461
    75
        // Retrieve project and platform
raster@14443
    76
        final AdaProject project = getProject();
raster@14488
    77
raster@14488
    78
        // Create Build/Dist folders
raster@14461
    79
        try {
raster@14461
    80
            createBuildRoot(project);
raster@14461
    81
            createDistRoot(project);
raster@14461
    82
        } catch (IOException ex) {
raster@14461
    83
            Exceptions.printStackTrace(ex);
raster@14461
    84
        }
raster@14461
    85
raster@14443
    86
        // Start build
raster@16367
    87
        ActionsUtil.getCompilerFactory(project, COMMAND_ID).Build();
raster@14352
    88
    }
raster@14352
    89
raster@14352
    90
    @Override
raster@14352
    91
    public boolean isActionEnabled(Lookup context) throws IllegalArgumentException {
raster@14461
    92
        final AdaProject project = getProject();
raster@14461
    93
        AdaPlatform platform = AdaProjectUtil.getActivePlatform(project);
raster@14443
    94
        if (platform == null) {
raster@14443
    95
            return false;
raster@14443
    96
        }
raster@16367
    97
        String mainFile = project.getEvaluator().getProperty(AdaProjectProperties.MAIN_FILE);
raster@16367
    98
        if (mainFile == null) {
raster@16367
    99
            return false;
raster@16367
   100
        }
raster@14443
   101
        return true;
raster@14352
   102
    }
raster@14461
   103
raster@14461
   104
    protected static FileObject findMainFile(final AdaProject project) {
raster@14461
   105
        final FileObject[] roots = project.getSourceRoots().getRoots();
raster@14461
   106
        final String mainFile = project.getEvaluator().getProperty(AdaProjectProperties.MAIN_FILE);
raster@14461
   107
        if (mainFile == null) {
raster@14461
   108
            return null;
raster@14461
   109
        }
raster@14461
   110
        FileObject fo = null;
raster@14461
   111
        for (FileObject root : roots) {
raster@14461
   112
            fo = root.getFileObject(mainFile);
raster@14461
   113
            if (fo != null) {
raster@14461
   114
                break;
raster@14461
   115
            }
raster@14461
   116
        }
raster@14461
   117
        return fo;
raster@14461
   118
    }
raster@14461
   119
raster@14461
   120
    private void createBuildRoot(final AdaProject project) throws IOException {
raster@15779
   121
        // Retrieve build path
raster@16367
   122
        final File projectDirectory = FileUtil.toFile(project.getProjectDirectory());
raster@16367
   123
        String buildPath = projectDirectory + File.separator + project.getEvaluator().getProperty(AdaProjectProperties.BUILD_DIR);
raster@15779
   124
        assert buildPath != null;
raster@15779
   125
        FileUtil.createFolder(new File(buildPath));
raster@14461
   126
    }
raster@14461
   127
raster@14461
   128
    private void createDistRoot(final AdaProject project) throws IOException {
raster@15779
   129
        // Retrieve dist path
raster@16367
   130
        final File projectDirectory = FileUtil.toFile(project.getProjectDirectory());
raster@16367
   131
        String distPath = projectDirectory + File.separator + project.getEvaluator().getProperty(AdaProjectProperties.DIST_DIR);
raster@15779
   132
        assert distPath != null;
raster@15779
   133
		FileUtil.createFolder(new File(distPath));
raster@14461
   134
    }
raster@14352
   135
}