<commit_before>/*
 * Copyright 2012 ios-driver committers.
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * 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 org.uiautomation.ios.server.utils;

import java.util.ArrayList;
import java.util.List;

import org.uiautomation.ios.exceptions.IOSAutomationException;
import org.uiautomation.ios.exceptions.IOSAutomationSetupException;

public class ClassicCommands {

  public static List<String> psgrep(String processName) throws IOSAutomationSetupException {
    List<String> s = new ArrayList<String>();
    s.add("ps");
    s.add("aux");

    Command com = new Command(s, false);
    Grep grep = new Grep(processName);
    com.registerListener(grep);
    com.executeAndWait();
    return grep.getMatching();
  }


  public static boolean isRunning(String processName) throws IOSAutomationSetupException {
    return psgrep(processName).size() > 0;
  }



  public static void killall(String processName) throws IOSAutomationSetupException {

    if (isRunning(processName)) {
      List<String> s = new ArrayList<String>();
      s.add("killall");
      s.add(processName);

      Command com = new Command(s, false);
      com.executeAndWait();
    }


  }

  public static List<String> getInstalledSDKs() throws IOSAutomationSetupException {
    List<String> c = new ArrayList<String>();
    c.add("xcodebuild");
    c.add("-showsdks");
    Command com = new Command(c, false);
    ShowSDKsPasrer l = new ShowSDKsPasrer();
    com.registerListener(l);
    com.executeAndWait();
    return l.getSDKs();
  }


  // TODO freynaud find the correct command
  public static String getDefaultSDK() {
    List<String> sdks = getInstalledSDKs();
    return sdks.get(sdks.size() - 1);
  }

}


class ShowSDKsPasrer implements CommandOutputListener {

  private List<String> sdks = new ArrayList<String>();
  private boolean ok = true;
  private final String pattern = "iphonesimulator";<commit_msg>getting xcode install location properly.<commit_after>/*
 * Copyright 2012 ios-driver committers.
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * 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 org.uiautomation.ios.server.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.uiautomation.ios.exceptions.IOSAutomationException;
import org.uiautomation.ios.exceptions.IOSAutomationSetupException;

public class ClassicCommands {

  public static List<String> psgrep(String processName) throws IOSAutomationSetupException {
    List<String> s = new ArrayList<String>();
    s.add("ps");
    s.add("aux");

    Command com = new Command(s, false);
    Grep grep = new Grep(processName);
    com.registerListener(grep);
    com.executeAndWait();
    return grep.getMatching();
  }


  public static boolean isRunning(String processName) throws IOSAutomationSetupException {
    return psgrep(processName).size() > 0;
  }



  public static void killall(String processName) throws IOSAutomationSetupException {

    if (isRunning(processName)) {
      List<String> s = new ArrayList<String>();
      s.add("killall");
      s.add(processName);

      Command com = new Command(s, false);
      com.executeAndWait();
    }


  }



  public static File getXCodeInstall() {
    List<String> cmd = new ArrayList<String>();
    cmd.add("/usr/bin/xcrun");
    cmd.add("-find");
    cmd.add("xcodebuild");

    Command c = new Command(cmd, false);
    c.executeAndWait();

    if (c.getStdOut().size() != 1) {
      throw new IOSAutomationException("cannot find XCode location." + c.getStdOut());
    }

    String path = c.getStdOut().get(0);

    String pattern = ".app/";
    int index = path.indexOf(pattern);
    String res = path.substring(0, index + pattern.length());
    return new File(res);

  }
  
  public static void main(String[] args) {
    System.out.println(getXCodeInstall());
  }

  public static List<String> getInstalledSDKs() throws IOSAutomationSetupException {
    List<String> c = new ArrayList<String>();
    c.add("xcodebuild");
    c.add("-showsdks");
    Command com = new Command(c, false);
    ShowSDKsPasrer l = new ShowSDKsPasrer();
    com.registerListener(l);
    com.executeAndWait();
    return l.getSDKs();
  }


  
}


class ShowSDKsPasrer implements CommandOutputListener {

  private List<String> sdks = new ArrayList<String>();
  private boolean ok = true;
  private final String pattern = "iphonesimulator";
{'fraction_non_alphanumeric': 0.10926099158091675, 'fraction_numerical': 0.00411599625818522, 'mean_word_length': 3.838009049773756, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6818668', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1546, 'n_words': 463}