Davy in France‎ > ‎

Blog

Random stuff I've found out or written.

Google calendar from the Command line.

posted 19 Mar 2014, 02:28 by Davy Jones   [ updated 19 Mar 2014, 02:43 ]


Create a small c# commandline project and compile, put it in the path and then you can make entries into google calendar using standard english.

c:> AddCal  Meeting tomorrow at 3pm with John

it then opens up chrome, or use Iexplore.exe, and google calendar with the details already filled in :)

static void Main(string[] args)
        {
            string googleQuickAdd = "http://www.google.com/calendar/event?ctext=+{0}+&action=TEMPLATE&pprop=HowCreated%3AQUICKADD";

            string build = string.Empty;
            foreach(string arg in args)
            {
                build += arg;
                build += " ";
            }

            string encoded = HttpUtility.HtmlEncode(build);
            
            Process.Start("Chrome.exe", string.Format(googleQuickAdd, encoded));
        }

Pointcuts and Spring.Net - How to inject that code the easy way.

posted 30 Nov 2012, 11:09 by Davy Jones

Recently I was looking for an easy example of how to add code dynamically based on an Attribute. I.e a Point Cut in Spring.Net.

 

Two days I searched, but do you think I could find a simple example that is complete? No it just doesn’t exist, so after a few hours pulling my hair out, here is my solution.

 

First the most important bit, what I want to do

 

class Program

    {

        static void Main(string[] args)

        {

            IApplicationContext ctx = new XmlApplicationContext("spring.config");

            var outputClass = (IOutputClass)ctx.GetObject("OutputClass");

            outputClass.DoWork();

           

            Console.ReadKey();

        }

    }


Most examples I have found start with the Classes then say, and this is how it works as XML. So I’m doing it different.

 

The ProxyCreator I’m using will scan all objects and apply advice to the ones that match.

 

<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop" />

 

This is the definition of the test class that I will be calling, the one with the do work method.

  <!-- My Class -->

  <object id="OutputClass" type="TestSpring.OutputClass, TestSpring"/>

 

The point cut, this tells that spring should look for an Attribute called CutAttribute and do STUFF with it.

   <!-- My AOP -->

  <object id="CutPointCut" type="Spring.Aop.Support.AttributeMatchMethodPointcut, Spring.Aop">

    <property name="Attribute" value="TestSpring.CutAttribute, TestSpring" />

  </object>

 

Now we tell spring that we have a CutAdvice class.

  <object id="CutAdvice" type="TestSpring.CutAdvice, TestSpring"/>

 

And for the ProxyCreator we tell it how to advise pointcuts!

  <object id="CutAspect" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop">

    <property name="Pointcut" ref="CutPointCut" />

    <property name="Advice" ref="CutAdvice"/>

  </object>

Notice that there is a lot of processing that is going on behind the scenes here as the Aspect doesn’t seem to be linked to the ProxyCreator at all.

 

Now lets define some classes to make it all work, First off the CutAttribute that we will put on our DoWork() method.

    [AttributeUsage(AttributeTargets.Method)]

    public class CutAttribute : Attribute

    {

    }

 

And the class that does the work.


    public class OutputClass : IOutputClass

    {

        [Cut]

        public void DoWork()

        {

            Console.WriteLine(" doing some work!");

        }

    }

 

Not forgetting that we also need an interface for spring to do it’s proxy magic around.

 

    public interface IOutputClass

    {

        void DoWork();

    }

 

And the Advice class, in most spring examples this is called interceptor or advisor or something that is there just to confuse you. This is the Cut that we will make in the code, the method gets passed in as invocation, we get to do stuff before the Proceed() call and after the Proceed() call.

 

    public class CutAdvice : IMethodInterceptor

    {

        public object Invoke(IMethodInvocation invocation)

        {

            Console.WriteLine(" Pre work!");

            var ret = invocation.Proceed();

            Console.WriteLine(" post work!");

            return ret;

        }

    }

 

The results, we get the code in the DoWork method with the advisor code’s modifications around result.


 Pre work!

 doing some work!

 post work!

 

Very very simple, here’s how you get the Attribute information inside the Advice class.

First the modification to the CutAttribute


[AttributeUsage(AttributeTargets.Method)]

    public class CutAttribute : Attribute

    {

        public CutAttribute()

        {

        }

 

        public CutAttribute(string message)

        {

            Message = message;

        }

 

        public string Message { get; set; }

    }


And the modification to the OutputClass


public class OutputClass : IOutputClass

    {

        [Cut(Message="Got to stop writing my blog and start")]

        public void DoWork()

        {

            Console.WriteLine(" doing some work!");

        }

    }


the modification to the Advice method. I’m ignoring the fact that the attribute might not exist, because if it doesn’t exist spring is not doing what we told it to do!


    public class CutAdvice : IMethodInterceptor

    {

        public object Invoke(IMethodInvocation invocation)

        {

            CutAttribute ca = (CutAttribute)invocation.Method.GetCustomAttributes(true).Where(x=> x is CutAttribute).FirstOrDefault();

            Console.Write(ca.Message);

            var ret = invocation.Proceed();

            return ret;

        }

    }


Hope this helps someone out there.

Davy.

Code injection.

posted 17 Jan 2012, 06:10 by Davy Jones

So I was told, "You can't use spring or any third party control".

  public static class AutoInjector
    {
        private static readonly object lockDictionary = new object();
        public static Dictionary<Type, Type> dictionary = new Dictionary<Type, Type>();

        public static void Register(Type interfaceType , Type concreteClass)
        {
            lock (lockDictionary)
            {
                if (dictionary.ContainsKey(interfaceType))
                {
                    throw new ArgumentOutOfRangeException(interfaceType.Name + " already exists in the dictionary");
                }
                else
                {
                    dictionary.Add(interfaceType, concreteClass);
                }
            }
        }


        public static T Get<T>()
        {
            return (T)Get(typeof(T));
        }

        private static object Get(Type interfaceType)
        {
            lock (lockDictionary)
            {
                if (!dictionary.ContainsKey(interfaceType))
                {
                    throw new ArgumentOutOfRangeException(interfaceType.Name + " does not exist in the dictionary");
                }

                Type t = dictionary[interfaceType];
                ConstructorInfo ci = t.GetConstructor(new Type[] { });
                object x = null;
                if (ci != null)
                {
                    x = ci.Invoke(new object[] { });
                }

                PropertyInfo[] pis = t.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (dictionary.ContainsKey(pi.PropertyType))
                    {
                        pi.SetValue(x, Get(pi.PropertyType), null);
                    }
                }

                return x;

                throw new Exception("Auto Injector did not complete");
            }
        }
    }

Windows Script Component

posted 7 Nov 2011, 06:59 by Davy Jones

An old technology, but a very useful one.

<?xml version="1.0"?>
<component>
   <registration  
    description="description"
    progid="someName.WSC"
    version="1.00"
    classid="{48474D70-4BC7-4c04-8346-28659D67FF82}"
    >
    </registration>
    <public>
        <method name="IsInstalled"/>
    </public>

    <script language="VBSCRIPT">
        <![CDATA[
            public function IsInstalled(name)
                IsInstalled = "File Not Found"
            end function
        ]]>
    </script>
</component>

More threading Fun.

posted 20 Jun 2011, 08:04 by Davy Jones

Recently I had to use the Background worker to sync a UI from a threaded computational part.

Lots of background workers can be very very painful, so I subclassed it.

    public class Task<T, R> : BackgroundWorker
    {
        public delegate R RunDelegate(T args);

        public event EventHandler<TaskCompletedEventArgs> TaskCompleted;

        public void Run(T args, RunDelegate codeToRun)
        {
            this.RunWorkerCompleted += (s, evt) =>
            {
                OnTaskCompleted(evt);
            };

            this.DoWork += (s, evt) =>
            {
                evt.Result = (R)codeToRun((T)evt.Argument);
            };

            this.RunWorkerAsync(args);
        }

        protected void OnTaskCompleted(RunWorkerCompletedEventArgs e)
        {
            EventHandler<TaskCompletedEventArgs> handler = TaskCompleted;
            if (handler != null)
            {
                handler(this, new Task<T, R>.TaskCompletedEventArgs(e));
            }
        }

        public class TaskCompletedEventArgs : EventArgs
        {
            public TaskCompletedEventArgs(RunWorkerCompletedEventArgs e)
            {
                Result = (R)e.Result;
                Error = e.Error;
                Cancelled = e.Cancelled;
            }

            public R Result { get; set; }

            public Exception Error { get; set; }

            public bool Cancelled { get; set; }
        }

iPad2 - Two week review.

posted 6 Jun 2011, 05:36 by Davy Jones   [ updated 6 Jun 2011, 05:57 ]

Two weeks ago I bought an iPad2. 16gb 3g wifi.

I'm pretty impressed by the applications, the navigation and the battery life. Good playback for films, games are fun to tap a screen and for the application I bought it for Amplitube I'm in love with it, really fun to play around with all the effects on my guitar without spending 150-300 euros for an effect that I might not want. 8 - 10 effects > price of ipad.

But, and this is a big point,  it is NOT a replacement for a laptop or a desktop computer, it's little more than a big iPhone without the telephone calls / sms.

and now the bad points.

iTunes,  what a pile of crap that is, trying to copy music to the iPad is a complete nightmare, you can't display folders or artists if their id3 tag isn't set, I spent years organising my play lists into folders for the album in a folder for the artist, with the correct name for the mp3.  And iTunes can't read that! Add to that the fact that half of my music doesn't show up at all. I thought apple was all about Ergonomics and ease of use.

iOS 4.3  after applying the update, many apps crash for no apparent reason, I have to go in and manually kill all the applications and then reopen the app from the start again. This is a pain in the ass and needs fixing.

Documentation,  what documentation!  there is nothing in the box, only a little thing that says  install iTunes, give over your credit card to jobs, then sync and charge. Where does it tell you about gestures / button, etc?  I had to go ask an iPhone user how to use my new toy.

Overall,

I'm impressed, I bought the iPad2 for two reasons,
 1. replace my broken eeepc
 2. amplitube 

If I can find a way to use the iPad without having to have the abortion that is iTunes on my computer I would like it a hell of a lot more.

Davy.



Using Twitter Atom Search Api.

posted 24 May 2011, 06:40 by Davy Jones

I subscribe to certain words around Paris using the below link.

http://search.twitter.com/search.atom?geocode=48.866666%2C2.333333%2C10km&q=

where the text after the q= is the search you want to perform.

ie to keep up todate with RATP,   do

http://search.twitter.com/search.atom?geocode=48.866666%2C2.333333%2C10km&q=ratp

And this will only return tweets around paris for the Ratp,

change the geo code to around your city.

Dj,


Visual Studio Macros

posted 30 Mar 2011, 05:17 by Davy Jones

I've been messing around in VS macros for a while now but one thing I always wanted to do but couldn't find help with was.

How do you run a macro after a Build?

And the answer is very very simple.

Open macro explorer,

right click you macro project and select edit.

on the left hand side is the project explorer..

open up EnvironmentEvents

in the top left drop down select the event group you want to trap,  ie BuildEvents

then in the top right drop down select the event you want to do.


Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone

Contrary to all documentation and blogs that I've seen on the net, this method ONLY works in the EnvironmentEvents module.

So here's my build event.

Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone

        DTE.ExecuteCommand("View.ErrorList")
        Dim er As ErrorList = DTE.ActiveWindow.Object

        If Action = vsBuildAction.vsBuildActionBuild Then
            If er IsNot Nothing AndAlso er.ErrorItems.Count = 0 Then
                DTE.ExecuteCommand("Tools.RunStyleCop")
            End If
        End If
    End Sub




Messing around with Localized strings

posted 2 Feb 2011, 08:53 by Davy Jones

I've had some free time this week so I decided to have a look at how to check that all strings have been localized.

public void ValidateTranslations(string[] supportedCultures)
        {
            CultureInfo ci = Thread.CurrentThread.CurrentUICulture;
            Type t = typeof(Properties.Resources);
            PropertyInfo[] pis = t.GetProperties(BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
            foreach (PropertyInfo pi in pis)
            {
                if (pi.PropertyType == typeof(string))
                {
                    ResourceManager rm = new ResourceManager(typeof(Properties.Resources));
                    string reference = rm.GetString(pi.Name, ci);
                    foreach (string culture in supportedCultures)
                    {
                        string compare = rm.GetString(pi.Name, CultureInfo.GetCultureInfo(culture));
                        if (string.IsNullOrEmpty(compare) || reference == compare)
                        {
                            throw new TranslatedStringException(CultureInfo.GetCultureInfo(culture), pi.Name, typeof(Properties.Resources));
                        }
                    }
                }
            }
}

I'm assuming that all the ressources are written in english and the development environment is in english, this can be changed by setting
CultureInfo ci = CultureInfo.GetCultureInfo("en");    
or to what ever language you write strings in.

It then uses your string resources in the Properties.Resources class to get the string in the default language and then for each of the supported cultures passed.

You could yield a not translated result if you needed, but this version I use in a unit test, so that I know if I add a string I need to add the translations into the other strings.

The resulting XML files with Google / Bing translated text in them can be given to a professional along with the orignial english version for correction..




Clearcase Rant again.

posted 21 Oct 2010, 02:53 by Davy Jones

I want to go back to a version of the code that I have used in the past, can I figure out how to do it.. can I f'ck.   So freaking hard to do stuff in clearcase I waste half a day trying to do something that I can do in seconds with any other source control.

1-10 of 40

Comments