扩展 C# .NET 应用程序 - 是否构建自定义脚本语言?
我需要为我的 C# 程序构建一个脚本接口,用于对嵌入式固件进行系统级测试。
我的应用程序包含与设备完全交互的库。有单独的库用于启动操作、获取输出和跟踪成功/失败。我的应用程序还有一个 GUI,用于管理多个设备并分配许多要运行的脚本。
对于测试人员(非程序员,但技术人员),我需要提供一个脚本接口,使他们能够提出不同的测试场景并运行它们。他们只会调用我的 API,然后将结果返回到我的程序(通过/失败和消息)。
我想要的一个非常基本的示例:
TURN_POWER_ON
TUNE_FREQUENCY frequency
WAIT 5
IF GET_FREQUENCY == frequency
REPORT_PASS "Successfully tuned to " + frequency
ELSE
REPORT_FAIL "Failed to tune to " + frequency
ENDIF
TURN_POWER_OFF
其中报告、功率和频率函数由我的 C# 库提供。
像 IronRuby 或 IronPython 这样的东西对此有好处吗?还是我应该构建自己的非常基本的语言?
当尝试包含一堆 .NET 编译的程序集时,Ruby/Python 代码是否会变得混乱?我希望它对于非程序员和程序员来说都易于学习和编码。
编辑:
感谢所有精彩的回复。我选择 IronPython 作为答案,因为它拥有最多的支持,但我将花一些时间分别研究 IronPython、Boo 和 IronRuby,看看测试人员更喜欢用什么来编写脚本。
I need to build a scripting interface for my C# program that does system level testing of embedded firmware.
My application contains libraries to fully interact with the devices. There are separate libraries for initiating actions, getting output and tracking success/failure. My application also has a GUI for managing multiple devices and assigning many scripts to be run.
For the testers (non-programmers, but technical), I need to provide a scripting interface that will allow them to come up with different scenarios for testing and run them. They are just going to call my APIs and then return a result to my program (pass/fail and message).
A very basic example of what I want:
TURN_POWER_ON
TUNE_FREQUENCY frequency
WAIT 5
IF GET_FREQUENCY == frequency
REPORT_PASS "Successfully tuned to " + frequency
ELSE
REPORT_FAIL "Failed to tune to " + frequency
ENDIF
TURN_POWER_OFF
Where the reporting, power and frequency functions are provided by my C# libraries.
Will something like IronRuby or IronPython be good for this, or should I just build my own very basic language?
Does the Ruby/Python code get messy when trying to include a bunch of .NET compiled assemblies? I want it to be easy to learn and code for non-programmers and programmers alike.
EDIT:
Thanks for all the great responses. I chose IronPython as the answer since it had the most support, but I'll spend a bit of time with each of IronPython, Boo and IronRuby to see what the testers would prefer to write scripts in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我听说 IronPython 非常适合这种类型的场景。我当然会冒着花几个小时进行快速概念验证的风险,看看结果如何。
Michael Foord 会很高兴地对 IronPython 在类似情况下的成功大加赞赏(特别是对于他来说,对于精通电子表格的用户),并且 他的书涵盖了 (IIRC) 有关从 .NET 托管它的一些建议。
I've heard very good things about IronPython for exactly this type of scenario. I'd certainly risk spending a few hours on a quick proof-of-concept, to see how it pans out.
Michael Foord will happily wax lyrical about the success of IronPython in similar cases (most specifically for him, for spreadsheet savvy users), and his book covers (IIRC) a few pointers about hosting it from .NET.
您可能需要查看 Boo,这是另一种在 CLR 上运行的托管语言,它特别适合构建 DSL 并使您的应用程序可编写脚本。
编译管道可以直接从语言本身扩展。
如果您想了解更多信息,阅读 Boo Manifesto 是一个很好的起点。
[编辑]我忘了提及Ayende Rahien正在写一本关于主题:
在 Boo 中构建领域特定语言
You may want to look at Boo, another managed language that runs on the CLR, and which is particularly well suited to build DSL and make your applications scriptable.
The compilation pipeline is directly extensible from the language itself.
Reading the Boo Manifesto is a good starting point if you want to learn more about it.
[Edit] I forgot to mention that Ayende Rahien is writing a complete book on the topic:
Building Domain Specific Languages in Boo
对于此类任务,可能值得考虑使用 PowerShell。它可以像任何 DLR 语言一样调用 .Net,并且在其 cmdlet (command-let) 概念中对任务具有更自然的语言类型分块。您必须使用 v1 的编译语言编写 cmdlet - 在 v2 中,v2 将从 Win7 开始推出,并在接下来的几个月内适用于旧版本(Vista/Win2k8 的 v2 现在处于 RC 版本),您可以构建这些命令直接在 PowerShell 中。
It might be worth considering PowerShell for this sort of task. That can call into .Net just as any of the DLR languages, and has a more natural language type chunking for tasks in its cmdlet (command-let) concept. You have to write the cmdlets in a compiled language at v1 -- in v2 which is being rolled out starting with Win7 and working to older releases in the next few months (v2 for Vista/Win2k8 is at RC now), you can build those in PowerShell directly.
我同意 Marc G 的观点,尽管值得一提的是一般概念是领域特定语言。虽然 IronRuby/IronPython 并不是严格特定于领域的,但它们功能齐全,可以让您继续实施。
Visual Studio 有 DSL 工具,还有“M”语法的东西你可以研究一下。
但是,是的,IronPython。
I agree with Marc G, though it's worth mentioning that the general concept is a Domain Specific Langugage. While IronRuby/IronPython aren't strictly domain-specific, they are full-featured and it would leave you to get on with your implementation.
Visual Studio has the DSL tools, and there's the 'M' Grammar stuff you can look into.
But yeah, IronPython.
从您要使用的 DSL 来看,我建议将 CUCUMBER 与 IronRuby 一起使用。
使用 Cucumber,测试人员编写的测试看起来像这样:
很容易使这种语言满足您的需求。
只需谷歌搜索“Cucumber and IronRuby”,您就会找到一些入门指南和博客文章。
From the DSL you're going for, I'd recommend to use CUCUMBER with IronRuby.
With Cucumber, testers write tests that look something like that:
It is very easy to make this language fit your needs.
Just google "Cucumber and IronRuby" and you'll find several guides and blog posts to get you started.
我们在我们的一个项目中使用嵌入式 Iron Python 进行定价公式。这就是真实样本的样子。
实施起来非常简单。例如,Max() 函数只是我们导入到 IronPython 引擎的自定义 C# 方法之一,在配置设置中使用它看起来很自然。
We are using embedded Iron Python for pricing formula in one of our project. This is how a real sample on how it looks like.
It is really straightforward to implement. The Max() function for example is just one of the custom C# method that we import to the IronPython engine and it looks natural to use in a configuration settings.
您可以使用 C# 本身作为脚本语言,如下所述 CrowsProgramming - .Net 中的运行时脚本
You could just use C# itself as the scripting language as described here CrowsProgramming - Runtime Scripting in .Net
IronRuby 是创建特定于领域的语言的最强大的语言,因为它的语法比 python 的语法更加灵活和宽容(您的用户会搞砸空格,并对调用方法的强制 () 感到恼火)。
您可以在 IronRuby 中编写示例脚本,它看起来像这样:
这是我们的测试人员当前用来针对我们的 UI 编写自动化测试的 DSL 示例
但是,就目前情况而言,IronPython 更加成熟并且具有更好的性能比 IronRuby 更好,所以您可能更喜欢使用它。
我强烈建议使用 IronPython 或 IronRuby,而不是创建自己的自定义语言......您将节省难以想象的工作量(和错误)
IronRuby is the most powerful for creating domain-specific languages, because it's syntax is much more flexible and forgiving than python's (your users are going to screw the whitespace up and get annoyed by the mandatory () to call methods).
You could write your sample script in IronRuby and it would look like this:
Here's a sample of a DSL our Testers are currently using to write automated tests against our UI
However, as things stand right now IronPython is much more mature and has much better performance than IronRuby, so you may prefer to use that.
I'd strongly recommend going with either IronPython or IronRuby over creating your own custom language... You'll save an unimaginable amount of effort (and bugs)
反讽也可能是一个很好的选择。
这是一个为给定图像动态创建动画的示例。
创建此类 DSL 的教程位于:编写你的第一个领域特定语言
这是一个旧教程,但作者仍然在这里维护他的库:Github讽刺
Irony could also be a good candidate here.
Here is an example which creates animation for the given image on the fly.
The tutorial for creating such DSL is here:Writing Your First Domain Specific Language
It is an old tutorial but the author still maintains his library here: Github Irony