/* * Generate_md5_sum.bsh - a BeanShell macro script for the * jEdit text editor - insert md5 sums for source files of a * PKGBUILD at current caret position (or replace selected text). * Copyright (C) 2007 Dmitry Stropaloff * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the jEdit program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: Generate_md5_sum.bsh,v 0.1 2007/02/11 16:39:05 h8 Exp $ */ import java.util.regex.*; setAccessibility(true); Pattern p; Matcher m; String bufText = buffer.getText(0, buffer.getLength()); String regExp = "^.*source\\s*=\\s*\\(([^)]{1,})\\).*$"; p = Pattern.compile(regExp, Pattern.MULTILINE | Pattern.DOTALL); m = p.matcher(bufText); if (!m.matches()) { Macros.message(view, "There is no \"source\" entry or it's incorrect."); break; } String srcs = m.group(1); regExp = "^.*pkgname=([a-z0-9-_]{1,}).*$"; p = Pattern.compile(regExp, Pattern.MULTILINE | Pattern.DOTALL); m = p.matcher(bufText); if (!m.matches()) { Macros.message(view, "There is no \"pkgname\" entry or it's broken."); break; } String pkgName = m.group(1); regExp = "^.*pkgver=([a-z0-9-_.]{1,}).*$"; p = Pattern.compile(regExp, Pattern.MULTILINE | Pattern.DOTALL); m = p.matcher(bufText); if (!m.matches()) { Macros.message(view, "There is no \"pkgver\" entry or it's broken."); break; } String pkgVer = m.group(1); srcs = srcs.replaceAll("\\n*", ""); srcs = srcs.replaceAll("\\s{2,}", ""); srcs = srcs.replaceAll("\\t*", ""); srcs = srcs.replaceAll("\\\\*", ""); String[] fileNames = srcs.split("\\s"); int index; String tmp; for (int i = 0; i < fileNames.length; i++) { tmp = fileNames[i]; index = tmp.lastIndexOf("/") + 1; if (index != -1) tmp = tmp.substring(index, tmp.length()); tmp = tmp.replace("$pkgname", pkgName); tmp = tmp.replace("${pkgname}", pkgName); tmp = tmp.replace("$pkgver", pkgVer); tmp = tmp.replace("${pkgver}", pkgVer); fileNames[i] = tmp; } Buffer tmpBuf; String md5; String tmpFName; String[] md5Sums = new String[fileNames.length]; String pathCmd = "cd " + buffer.getDirectory(); runInSystemShell(view, pathCmd); for (int i = 0; i < fileNames.length; i++) { md5Cmd = "md5sum " + fileNames[i] + " > " + fileNames[i] + ".md5"; runInSystemShell(view, md5Cmd); waitForConsole(view); clearConsole(view); tmpFName = buffer.getDirectory() + fileNames[i] + ".md5"; tmpBuf = jEdit.openTemporary(view, null, tmpFName, false); while (!tmpBuf.isLoaded()) {} md5 = tmpBuf.getText(0, tmpBuf.getLength()); md5 = md5.replaceAll("\\s.*$", ""); md5Sums[i] = md5; tmpBuf.close(); rm(tmpFName); } String md5Text = "md5sums=("; for (int i = 0; i < md5Sums.length; i++) md5Text = md5Text.concat("'").concat(md5Sums[i]).concat("'"); md5Text = md5Text + ")"; md5Text = md5Text.replaceAll("''", "' '"); view.getTextArea().setSelectedText(md5Text); /* Macro index data (in DocBook format) Generate_md5_sum.bsh Inserts md5 sums for source files of a PKGBUILD */ // end Generate_md5_sum.bsh