• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.googlecode.android_scripting;
18 
19 import com.googlecode.android_scripting.interpreter.Interpreter;
20 import com.googlecode.android_scripting.interpreter.InterpreterConfiguration;
21 import com.googlecode.android_scripting.interpreter.InterpreterConstants;
22 
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.List;
34 
35 /**
36  * Manages storage and retrieval of scripts on the file system.
37  *
38  * @author Damon Kohler (damonkohler@gmail.com)
39  */
40 public class ScriptStorageAdapter {
41 
ScriptStorageAdapter()42   private ScriptStorageAdapter() {
43     // Utility class.
44   }
45 
46   /**
47    * Writes data to the script by name and overwrites any existing data.
48    */
writeScript(File script, String data)49   public static void writeScript(File script, String data) {
50     if (script.getParent() == null) {
51       script = new File(InterpreterConstants.SCRIPTS_ROOT, script.getPath());
52     }
53     try {
54       FileWriter stream = new FileWriter(script, false /* overwrite */);
55       BufferedWriter out = new BufferedWriter(stream);
56       out.write(data);
57       out.close();
58     } catch (IOException e) {
59       Log.e("Failed to write script.", e);
60     }
61   }
62 
63   /**
64    * Returns a list of all available script {@link File}s.
65    */
listAllScripts(File dir)66   public static List<File> listAllScripts(File dir) {
67     if (dir == null) {
68       dir = new File(InterpreterConstants.SCRIPTS_ROOT);
69     }
70     if (dir.exists()) {
71       List<File> scripts = Arrays.asList(dir.listFiles());
72       Collections.sort(scripts, new Comparator<File>() {
73         @Override
74         public int compare(File file1, File file2) {
75           if (file1.isDirectory() && !file2.isDirectory()) {
76             return -1;
77           } else if (!file1.isDirectory() && file2.isDirectory()) {
78             return 1;
79           }
80           return file1.compareTo(file2);
81         }
82       });
83       return scripts;
84     }
85     return new ArrayList<File>();
86   }
87 
88   /**
89    * Returns a list of script {@link File}s from the given folder for which there is an interpreter
90    * installed.
91    */
listExecutableScripts(File directory, InterpreterConfiguration config)92   public static List<File> listExecutableScripts(File directory, InterpreterConfiguration config) {
93     // NOTE(damonkohler): Creating a LinkedList here is necessary in order to be able to filter it
94     // later.
95     List<File> scripts = new LinkedList<File>(listAllScripts(directory));
96     // Filter out any files that don't have interpreters installed.
97     for (Iterator<File> it = scripts.iterator(); it.hasNext();) {
98       File script = it.next();
99       if (script.isDirectory()) {
100         continue;
101       }
102       Interpreter interpreter = config.getInterpreterForScript(script.getName());
103       if (interpreter == null || !interpreter.isInstalled()) {
104         it.remove();
105       }
106     }
107     return scripts;
108   }
109 
110   /**
111    * Returns a list of all (including subfolders) script {@link File}s for which there is an
112    * interpreter installed.
113    */
listExecutableScriptsRecursively(File directory, InterpreterConfiguration config)114   public static List<File> listExecutableScriptsRecursively(File directory,
115       InterpreterConfiguration config) {
116     // NOTE(damonkohler): Creating a LinkedList here is necessary in order to be able to filter it
117     // later.
118     List<File> scripts = new LinkedList<File>();
119     List<File> files = listAllScripts(directory);
120 
121     // Filter out any files that don't have interpreters installed.
122     for (Iterator<File> it = files.iterator(); it.hasNext();) {
123       File file = it.next();
124       if (file.isDirectory()) {
125         scripts.addAll(listExecutableScriptsRecursively(file, config));
126       }
127       Interpreter interpreter = config.getInterpreterForScript(file.getName());
128       if (interpreter != null && interpreter.isInstalled()) {
129         scripts.add(file);
130       }
131     }
132     Collections.sort(scripts);
133     return scripts;
134   }
135 }