aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/actions/BitbakeImportAction.java
blob: e8e093fede93b6cf91dc9f62f29e909ede121815 (plain)
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
/*****************************************************************************
 * Copyright (c) 2009 Ken Gilmer
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ken Gilmer - initial API and implementation
 *******************************************************************************/
package org.openembedded.bc.ui.actions;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.openembedded.bc.bitbake.BBCommonVars;
import org.openembedded.bc.bitbake.BBRecipe;
import org.openembedded.bc.ui.Activator;


public  class BitbakeImportAction extends AbstractBitbakeCommandAction {

	private class ImportJob extends Job {

		public ImportJob() {
			super(getJobTitle());
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			
			try {
				BBRecipe br = new BBRecipe(bbs, recipe.getLocationURI().getPath());
				br.initialize();
				String filePath = (String) br.get(BBCommonVars.S);
				
				//"${WORKDIR}/${PN}-${PV}"
				if (filePath == null) {
					filePath = ((String) br.get(BBCommonVars.WORKDIR)) + File.separator + ((String) br.get(BBCommonVars.PN)) + "-" + ((String) br.get(BBCommonVars.PV));
				}
				
				String projectName = (String) br.get(BBCommonVars.PN);
				
				if (filePath == null || projectName == null) {
					return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to parse recipe file.");
				}
				
				File workdir = new File(filePath);
				
				if (workdir.exists() && workdir.isFile()) {
					return new Status(IStatus.ERROR, Activator.PLUGIN_ID, workdir.getPath() + " is an invalid workdir.");
				}
			
				if (!workdir.exists()) {
					execCommands(new String[] {"bitbake -c patch -b " + recipe.getLocationURI().getPath()}, monitor);
				}
				
				if (!workdir.exists()) {
					return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to retrieve sources from BitBake.  Consult console.");
				}
				
				IProjectDescription desc = ResourcesPlugin.getWorkspace().newProjectDescription(projectName);
				IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
				IProject proj = wsroot.getProject(projectName);
				proj.create(desc, monitor);		
				proj.open(monitor);
				
				String copyCmd = "cp -r " + workdir.getAbsolutePath() + File.separator + "* \"" + proj.getLocationURI().getPath() + "\"";
				execCommands(new String[] {copyCmd} , monitor);
				
				proj.refreshLocal(IResource.DEPTH_INFINITE, monitor);
				
			} catch (Exception e) {
				e.printStackTrace();
				return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to create project.", e);
			}
			
			return Status.OK_STATUS;
		}
		
	}

	@Override
	public String [] getCommands() {
		return null;
	}
	

	@Override
	public Job getJob() {
		return new ImportJob();
	}
	
	@Override
	public String getJobTitle() {
		return "Importing " + recipe.getName();
	}
}