Home

Running LINQPad scripts from command prompt and PUB XML OPF file creation

edited March 2012
Is there a way to run scripts you create in LINQpad as command scripts from windows? Are there simple ways of compiling them through .NET somehow without opening VS? I've looked into this in the past and have never found a simple solution.

The ability to call .NET functions from LINQpad is awesome. Below is a script I created in LINQpad that creates an EPUB OPF file.

So THANK YOU JOE!

' Get files
Dim dFiles As New Dictionary(Of Integer, String)
dim sPath as string = "C:\Files2012_Test\2012-03-07"
Dim dirInfo As New IO.DirectoryInfo(sPath)

Dim cnt As Integer = 0
For Each file As FileInfo In dirInfo.GetFiles("*.html", SearchOption.TopDirectoryOnly)
cnt += 1
dFiles.Add(cnt, file.Name)
Next

Dim pair As KeyValuePair(Of Integer, String)
For Each pair In dFiles
Debug.Print("{0}, {1}", pair.Key, pair.Value)
Next

' end get files

Dim ns As XNamespace = "http://www.idpf.org/2007/opf"
Dim dc As XNamespace = "http://purl.org/dc/elements/1.1/"
Dim opf As XNamespace = "http://www.idpf.org/2007/opf"

Dim BookTitle As String = "Anatomy and Physiology"
Dim BookId As String = "123"
Dim TocTitle As String = "Chapters"
Dim BookAuthorName As String = "Smith"
Dim tocHtmlFilename As String = "htmlfilename"
Dim textGuide As String = "study"
Dim PathGetFileNameFilename As String = ""

'TODO : setup all metadata
Dim metadata As New XElement(ns + "metadata", _
New XAttribute(XNamespace.Xmlns + "dc", dc), _
New XAttribute(XNamespace.Xmlns + "opf", opf), _
New XElement(dc + "title", BookTitle), _
New XElement(dc + "language", "en-us"), _
New XElement("meta", _
New XAttribute("name", "cover"), _
New XAttribute("content", "My_Cover")), _
New XElement(dc + "identifier", _
New XAttribute("id", BookId), _
New XAttribute(opf + "scheme", "ISBN"), "123456789"), _
New XElement(dc + "creator", BookAuthorName), _
New XElement(dc + "publisher", "McGraw Hill"), _
New XElement(dc + "subject", "McGraw Hill"), _
New XElement(dc + "date", DateTime.Today))

Dim manifest as new XElement(ns + "manifest")

For Each pair In dFiles
manifest.Add(new XElement(ns + "item", _
New XAttribute("id",pair.Key), _
New XAttribute("href",pair.Value)))
Next


Dim spine As New XElement(ns + "spine", _
New XAttribute("toc", "My_Table_of_Contents"), _
New XElement(ns + "itemref", _
New XAttribute("idref", "item1")), _
New XElement(ns + "itemref", _
New XAttribute("idref", "item2")))

Dim guide As New XElement(ns + "guide", New XElement(ns + "reference", New XAttribute("type", "toc"), New XAttribute("title", TocTitle), New XAttribute("href", tocHtmlFilename)), New XElement(ns + "reference", New XAttribute("type", "text"), New XAttribute("title", TocTitle), New XAttribute("href", PathGetFileNameFilename + (If((textGuide Is Nothing), "", "#" + textGuide)))))

Dim package As New XElement(ns + "package", New XAttribute("version", "2.0"), New XAttribute("unique-identifier", BookId), metadata, manifest, spine, _
guide)

Dim opfDoc As New XDocument(package)

opfDoc.Save(Path.ChangeExtension(sPath + "\aTestfromvb", ".opf"))
' Console.WriteLine(Path.ChangeExtension(filename, ".opf" + ": file created."));

Comments

  • A command line/automation interface has been much requested and I believe Joe is working on it.

    You could also vote here. Joe started a thread in the old forum here.
  • Would you prefer to call LINQPad from the command-line, or have an option to compile a script to an executable?
  • Hi Joe. If you could create a compile script to exe that would certainly be very useful to me. Using LINQpad as a command line interpreter would necessitate LINQPad being installed on every machine that ran the script, I would think. Anyway, the ability to use LINQpad as a .NET scripting environment is incredible to me--I wish I realized it sooner. If you put in compile to exe it could grow LINQpad beyond LINQ. I could see many uses for it, especially people who want to run scripts on Azure, AWS, etc. I often find VS overkill for stuff I want to do, and definitely for experimenting. That's my 2-cents! Thanks!
  • The compile-to-EXE option is the simplest to implement and is probably how it will end up being done.
  • I have done something like this before in a personal hobby project. I used the same technique that was used in SnippetCompiler. Basically, when the script is run, feed code to csc.exe to create an executable, run the executable and capture the output, show the result to the user and delete the interim executable. Not the most efficent way to do it but certaily simple.
Sign In or Register to comment.