<commit_before>package flixel.system;

#if macro
import haxe.io.BytesOutput;
import haxe.io.Eof;
import haxe.macro.Context;
import haxe.macro.Expr;
import sys.io.Process;
using StringTools;

/**
 * Heavily inspired by HaxePunk's HaxeLibInfo.hx
 */ 
class GitSHA
{
	public static function buildGitSHA(library:String):Array<Field>
	{
		var fields:Array<Field> = Context.getBuildFields();
		
		var libraryPath = getLibraryPath(library);
		var sha = "";
		
		if (libraryPath.indexOf('$library/git') != -1)
		{
			sha = getGitSHA(libraryPath);
		}
		
		fields.push({
			name: "sha",
			doc: null,
			meta: [],
			access: [Access.APublic, Access.AStatic],
			kind: FieldType.FProp("default", "null", macro:Dynamic, macro $v{sha}),
			pos: Context.currentPos()
		});
		
		return fields;
	}
	
	public static function getLibraryPath(library:String):String
	{
		var output = getProcessOutput("haxelib", ["path", library]);
		
		var result = "";
		var lines = output.split("\n");
		
		for (i in 1...lines.length)
		{
			if (lines[i].trim() == '-D $library')
			{
				result = lines[i - 1].trim(); 
			}
		}
		
		return result;
	}
	
	public static function getGitSHA(path:String):String
	{
		var oldWd = Sys.getCwd();
		
		Sys.setCwd(path);
		var sha = getProcessOutput("git", ["rev-parse", "HEAD"]);
		
		Sys.setCwd(oldWd);
		return sha.substring(0, 10);
	}
	
	public static function getProcessOutput(cmd:String, args:Array<String>):String
	{
		var output = "";
		
		try
		{
			var process = new Process(cmd, args);
			var buffer = new BytesOutput();
			
			while (true)
			{
				try
				{
					var currentOutput = process.stdout.readAll(1024);
					buffer.write(currentOutput);
					if (currentOutput.length == 0)
					{
						break;
					}
				}
				catch (e:Eof)
				{
					break;<commit_msg>GitSHA: compatibility with libs not installed via haxelib git<commit_after>package flixel.system;

#if macro
import haxe.io.BytesOutput;
import haxe.io.Eof;
import haxe.macro.Context;
import haxe.macro.Expr;
import sys.io.Process;
using StringTools;

/**
 * Heavily inspired by HaxePunk's HaxeLibInfo.hx
 */ 
class GitSHA
{
	public static function buildGitSHA(library:String):Array<Field>
	{
		var fields:Array<Field> = Context.getBuildFields();
		
		var libraryPath = getLibraryPath(library);
		var sha = getGitSHA(libraryPath);
		
		fields.push({
			name: "sha",
			doc: null,
			meta: [],
			access: [Access.APublic, Access.AStatic],
			kind: FieldType.FProp("default", "null", macro:Dynamic, macro $v{sha}),
			pos: Context.currentPos()
		});
		
		return fields;
	}
	
	public static function getLibraryPath(library:String):String
	{
		var output = getProcessOutput("haxelib", ["path", library]);
		
		var result = "";
		var lines = output.split("\n");
		
		for (i in 1...lines.length)
		{
			if (lines[i].trim() == '-D $library')
			{
				result = lines[i - 1].trim(); 
			}
		}
		
		return result;
	}
	
	public static function getGitSHA(path:String):String
	{
		var oldWd = Sys.getCwd();
		
		Sys.setCwd(path);
		var sha = getProcessOutput("git", ["rev-parse", "HEAD"]);
		var shaRegex = ~/[a-f0-9]{40}/g;
		if (!shaRegex.match(sha))
		{
			sha = "";
		}
		
		Sys.setCwd(oldWd);
		return sha.substring(0, 10);
	}
	
	public static function getProcessOutput(cmd:String, args:Array<String>):String
	{
		var output = "";
		
		try
		{
			var process = new Process(cmd, args);
			var buffer = new BytesOutput();
			
			while (true)
			{
				try
				{
					var currentOutput = process.stdout.readAll(1024);
					buffer.write(currentOutput);
					if (currentOutput.length == 0)
					{
						break;
					}
				}
				catch (e:Eof)
				{
					break;
{'fraction_non_alphanumeric': 0.14998621450234353, 'fraction_numerical': 0.006892748828232699, 'mean_word_length': 3.170114942528736, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '885741', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1251, 'n_words': 277}