aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/exportgradle/ProjectSetupBuilder.java
blob: 1fd6b74f6d9d3d202a25464a2b1667db8577d70e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.wizards.exportgradle;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.eclipse.adt.AdtConstants;
import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper;
import com.android.ide.eclipse.adt.internal.sdk.ProjectState;
import com.android.ide.eclipse.adt.internal.sdk.ProjectState.LibraryState;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Class to setup the project and its modules.
 */
public class ProjectSetupBuilder {

    private final static class InternalException extends Exception {
        private static final long serialVersionUID = 1L;

        InternalException(String message) {
            super(message);
        }
    }

    private boolean mCanFinish = false;
    private boolean mCanGenerate = false;
    private final List<GradleModule> mOriginalModules = Lists.newArrayList();
    private final Map<IJavaProject, GradleModule> mModules = Maps.newHashMap();
    private IPath mCommonRoot;
    private ExportStatus mStatus;

    public ProjectSetupBuilder() {

    }

    public void setCanGenerate(boolean generate) {
        mCanGenerate = generate;
    }

    public void setCanFinish(boolean canFinish) {
        mCanFinish = canFinish;
    }

    public boolean canFinish() {
        return mCanFinish;
    }

    public boolean canGenerate() {
        return mCanGenerate;
    }

    public void setStatus(ExportStatus status) {
        mStatus = status;
    }

    public ExportStatus getStatus() {
        return mStatus;
    }

    @NonNull
    public String setProject(@NonNull List<IJavaProject> selectedProjects)
            throws CoreException {
        mModules.clear();

        // build a list of all projects that must be included. This is in case
        // some dependencies have not been included in the selected projects. We also include
        // parent projects so that the full multi-project setup is correct.
        // Note that if two projects are selected that are not related, both will be added
        // in the same multi-project anyway.
        try {
            for (IJavaProject javaProject : selectedProjects) {
                GradleModule module;

                if (javaProject.getProject().hasNature(AdtConstants.NATURE_DEFAULT)) {
                    module = processAndroidProject(javaProject);
                } else {
                    module = processJavaProject(javaProject);
                }

                mOriginalModules.add(module);
            }

            Collection<GradleModule> modules = mModules.values();
            computeRootAndPaths(modules);

            return null;
        } catch (InternalException e) {
            return e.getMessage();
        }
    }

    @NonNull
    public Collection<GradleModule> getModules() {
        return mModules.values();
    }

    public int getModuleCount() {
        return mModules.size();
    }

    @Nullable
    public IPath getCommonRoot() {
        return mCommonRoot;
    }

    @Nullable
    public GradleModule getModule(IJavaProject javaProject) {
        return mModules.get(javaProject);
    }

    public boolean isOriginalProject(@NonNull IJavaProject javaProject) {
        GradleModule module = mModules.get(javaProject);
        return mOriginalModules.contains(module);
    }

    @NonNull
    public List<GradleModule> getOriginalModules() {
        return mOriginalModules;
    }

    @Nullable
    public List<GradleModule> getShortestDependencyTo(GradleModule module) {
        return findModule(module, mOriginalModules);
    }

    @Nullable
    public List<GradleModule> findModule(GradleModule toFind, GradleModule rootModule) {
        if (toFind == rootModule) {
            List<GradleModule> list = Lists.newArrayList();
            list.add(toFind);
            return list;
        }

        List<GradleModule> shortestChain = findModule(toFind, rootModule.getDependencies());

        if (shortestChain != null) {
            shortestChain.add(0, rootModule);
        }

        return shortestChain;
    }

    @Nullable
    public List<GradleModule> findModule(GradleModule toFind, List<GradleModule> modules) {
        List<GradleModule> currentChain = null;

        for (GradleModule child : modules) {
            List<GradleModule> newChain = findModule(toFind, child);
            if (currentChain == null) {
                currentChain = newChain;
            } else if (newChain != null) {
                if (currentChain.size() > newChain.size()) {
                    currentChain = newChain;
                }
            }
        }

        return currentChain;
    }

    @NonNull
    private GradleModule processAndroidProject(@NonNull IJavaProject javaProject)
            throws InternalException, CoreException {

        // get/create the module
        GradleModule module = createModuleOnDemand(javaProject);
        if (module.isConfigured()) {
            return module;
        }

        module.setType(GradleModule.Type.ANDROID);

        ProjectState projectState = Sdk.getProjectState(javaProject.getProject());
        assert projectState != null;

        // add library project dependencies
        List<LibraryState> libraryProjects = projectState.getLibraries();
        for (LibraryState libraryState : libraryProjects) {
            ProjectState libProjectState = libraryState.getProjectState();
            if (libProjectState != null) {
                IJavaProject javaLib = getJavaProject(libProjectState);
                if (javaLib != null) {
                    GradleModule libModule = processAndroidProject(javaLib);
                    module.addDependency(libModule);
                } else {
                    throw new InternalException(String.format(
                            "Project %1$s is missing. Needed by %2$s.\n" +
                            "Make sure all dependencies are opened.",
                            libraryState.getRelativePath(),
                            javaProject.getProject().getName()));
                }
            } else {
                throw new InternalException(String.format(
                        "Project %1$s is missing. Needed by %2$s.\n" +
                        "Make sure all dependencies are opened.",
                        libraryState.getRelativePath(),
                        javaProject.getProject().getName()));
            }
        }

        // add java project dependencies
        List<IJavaProject> javaDepProjects = getReferencedProjects(javaProject);
        for (IJavaProject javaDep : javaDepProjects) {
            GradleModule libModule = processJavaProject(javaDep);
            module.addDependency(libModule);
        }

        return module;
    }

    @NonNull
    private GradleModule processJavaProject(@NonNull IJavaProject javaProject)
            throws InternalException, CoreException {
        // get/create the module
        GradleModule module = createModuleOnDemand(javaProject);

        if (module.isConfigured()) {
            return module;
        }

        module.setType(GradleModule.Type.JAVA);

        // add java project dependencies
        List<IJavaProject> javaDepProjects = getReferencedProjects(javaProject);
        for (IJavaProject javaDep : javaDepProjects) {
            // Java project should not reference Android project!
            if (javaDep.getProject().hasNature(AdtConstants.NATURE_DEFAULT)) {
                throw new InternalException(String.format(
                        "Java project %1$s depends on Android project %2$s!\n" +
                        "This is not a valid dependency",
                        javaProject.getProject().getName(), javaDep.getProject().getName()));
            }
            GradleModule libModule = processJavaProject(javaDep);
            module.addDependency(libModule);
        }

        return module;
    }

    private void computeRootAndPaths(Collection<GradleModule> modules) throws InternalException {
        // compute the common root.
        mCommonRoot = determineCommonRoot(modules);

        // compute all the relative paths.
        for (GradleModule module : modules) {
            String path = getGradlePath(module.getJavaProject().getProject().getLocation(),
                    mCommonRoot);

            module.setPath(path);
        }
    }

    /**
     * Finds the common parent directory shared by this project and all its dependencies.
     * If there's only one project, returns the single project's folder.
     * @throws InternalException
     */
    @NonNull
    private static IPath determineCommonRoot(Collection<GradleModule> modules)
            throws InternalException {
        IPath commonRoot = null;
        for (GradleModule module : modules) {
            if (commonRoot == null) {
                commonRoot = module.getJavaProject().getProject().getLocation();
            } else {
                commonRoot = findCommonRoot(commonRoot,
                        module.getJavaProject().getProject().getLocation());
            }
        }

        return commonRoot;
    }

    /**
     * Converts the given path to be relative to the given root path, and converts it to
     * Gradle project notation, such as is used in the settings.gradle file.
     */
    @NonNull
    private static String getGradlePath(IPath path, IPath root) {
        IPath relativePath = path.makeRelativeTo(root);
        String relativeString = relativePath.toOSString();
        return ":" + relativeString.replaceAll(Pattern.quote(File.separator), ":"); //$NON-NLS-1$
    }

    /**
     * Given two IPaths, finds the parent directory of both of them.
     * @throws InternalException
     */
    @NonNull
    private static IPath findCommonRoot(@NonNull IPath path1, @NonNull IPath path2)
            throws InternalException {
        if (path1.getDevice() != null && !path1.getDevice().equals(path2.getDevice())) {
            throw new InternalException(
                    "Different modules have been detected on different drives.\n" +
                    "This prevents finding a common root to all modules.");
        }

        IPath result = path1.uptoSegment(0);

        final int count = Math.min(path1.segmentCount(), path2.segmentCount());
        for (int i = 0; i < count; i++) {
            if (path1.segment(i).equals(path2.segment(i))) {
                result = result.append(Path.SEPARATOR + path2.segment(i));
            }
        }
        return result;
    }

    @Nullable
    private IJavaProject getJavaProject(ProjectState projectState) {
        try {
            return BaseProjectHelper.getJavaProject(projectState.getProject());
        } catch (CoreException e) {
            return null;
        }
    }

    @NonNull
    private GradleModule createModuleOnDemand(@NonNull IJavaProject javaProject) {
        GradleModule module = mModules.get(javaProject);
        if (module == null) {
            module = new GradleModule(javaProject);
            mModules.put(javaProject, module);
        }

        return module;
    }

    @NonNull
    private static List<IJavaProject> getReferencedProjects(IJavaProject javaProject)
            throws JavaModelException, InternalException {

        List<IJavaProject> projects = Lists.newArrayList();

        IClasspathEntry entries[] = javaProject.getRawClasspath();
        for (IClasspathEntry classpathEntry : entries) {
            if (classpathEntry.getContentKind() == IPackageFragmentRoot.K_SOURCE
                    && classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                // found required project on build path
                String subProjectRoot = classpathEntry.getPath().toString();
                IJavaProject subProject = getJavaProject(subProjectRoot);
                // is project available in workspace?
                if (subProject != null) {
                    projects.add(subProject);
                } else {
                    throw new InternalException(String.format(
                            "Project '%s' is missing project dependency '%s' in Eclipse workspace.\n" +
                            "Make sure all dependencies are opened.",
                            javaProject.getProject().getName(),
                            classpathEntry.getPath().toString()));
                }
            }
        }

        return projects;
    }

    /**
     * Get Java project for given root.
     */
    @Nullable
    private static IJavaProject getJavaProject(String root) {
        IPath path = new Path(root);
        if (path.segmentCount() == 1) {
            return getJavaProjectByName(root);
        }
        IResource resource = ResourcesPlugin.getWorkspace().getRoot()
                .findMember(path);
        if (resource != null && resource.getType() == IResource.PROJECT) {
            if (resource.exists()) {
                return (IJavaProject) JavaCore.create(resource);
            }
        }
        return null;
    }

    /**
     * Get Java project from resource.
     */
    private static IJavaProject getJavaProjectByName(String name) {
        try {
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
            if (project.exists()) {
                return JavaCore.create(project);
            }
        } catch (IllegalArgumentException iae) {
        }
        return null;
    }
}